我目前在VB.NET express for desktop,2013中工作。我很難將SQL數據綁定到一個數組的循環上的一些datagridviews。我收到一個對象爲null的錯誤,因爲在直接投射線上它沒有拉動datagridview。我在一個選項卡控制工具上有多個datagridview,每個選項卡有一個datagridview。這裏是我的代碼:直接投給datagridview的空錯誤
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
打開選項嚴格'「2」,「3」,「4」'不是整數 – Plutonix
@Plutonix我打開選項嚴格它也改變了2,3,4,並且代碼仍然在竊聽我。 – Cheddar
我無法重現該問題。假設你有'Option Infer On',那麼'RelativeDGV'將是'DataGridView'類型(否則你應該有'Dim RelativeDGV As DataGridView = DirectCast(.....)')。你確定DataGridLine2,DataGridLine3等都存在,屬於DataGridView類型,並且是表單控件集合的成員(不在「GroupBox」或其他容器中)? – Blackwood