我目前正在使用 VB.NET express for desktop,2013。我很难将 SQL 数据绑定到带有数组的循环上的一些 datagridviews。我收到一个对象空错误,这是因为在直接投射线上它没有拉数据网格视图。我在一个选项卡控制工具上有多个数据网格视图,每个选项卡一个数据网格视图。这是我的代码:
try
Dim array() As Integer = {"2", "3", "4", "7", "8", "10", "11", "12"}
For Each value As Integer In array
Dim RelativeDGV = DirectCast(Me.Controls("DataGridLine" & value), DataGridView)
Using conn1 As New SqlConnection(connstring)
conn1.Open()
Using comm1 As New SqlCommand("SELECT LineNumber FROM tableA where LineNumber = @LineNumber", conn1)
comm1.Parameters.AddWithValue("@LineNumber", value)
Dim dt As New DataTable
Dim sql As New SqlDataAdapter(comm1)
sql.Fill(dt)
RelativeDGV.DataSource = dt
End Using
conn1.Close()
End Using
Next
Catch ex As Exception
MsgBox(ex.ToString)
End Try
错误在线
Dim RelativeDGV = DirectCast(Me.Controls("DataGridLine" & value), DataGridView)
但是空错误不会触发,直到
RelativeDGV.DataSource = dt