0

データ テーブルが正しく機能していません。動的テーブル レイアウトが表示されないことさえあります。そして、何がうまくいかなかったのか、私にはまったくわかりません。ちなみに初心者です。

Public Class ListItem
Dim dt As System.Data.DataTable


Private Sub ListItem_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim conn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source =|DataDirectory|\SMS.accdb"
    Dim sqlstr As String = "Select * from ItemList"
    Dim dtad As New OleDb.OleDbDataAdapter(sqlstr, conn)
    dtad.Fill(dt)
    dtad.Dispose()
End Sub

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim ListTable As New TableLayoutPanel()
    ListTable.AutoSize = True
    ListTable.AutoSizeMode = Windows.Forms.AutoSizeMode.GrowAndShrink
    ListTable.Location = New Point(20, 20)
    ListTable.BackColor = Color.White
    ListTable.ColumnCount = CInt(dt.Columns.Count)
    ListTable.RowCount = CInt(dt.Rows.Count)
    ListTable.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single


    For colindex = 0 To dt.Columns.Count - 1
        For rowindex = 0 To 0
            Dim newlabel As New Label()
            newlabel.Location = New Point(10, 10)
            newlabel.Name = "label" & colindex
            newlabel.Font = New Drawing.Font("Microsoft Sans Serif", 16, FontStyle.Underline)
            newlabel.Text = dt.Columns(colindex).ColumnName
            newlabel.AutoSize = True
            ListTable.Controls.Add(newlabel, colindex, rowindex)
        Next
    Next

    Controls.Add(ListTable)
End Sub
End Class

それは私にこのエラーを与えます:

An unhandled exception of type 'System.NullReferenceException' occurred in StockManagementSystem.exe

Additional information: Object reference not set to an instance of an object.
4

1 に答える 1

1

クラス内の最初の行。これから変更します:

Dim dt As System.Data.DataTable

これに:

Dim dt As New System.Data.DataTable
于 2013-04-08T14:28:10.597 に答える