[英]Direct Cast giving null error for datagridview
I am currently working in VB.NET express for desktop, 2013. I am having a hard time binding SQL data to some datagridviews on a loop with an array. 我目前正在2013年面向台式机的VB.NET Express中工作。我很难将SQL数据绑定到具有数组的循环上的某些datagridviews。 I am receiving an object null error and its because on the direct cast line its not pulling the datagridview.
我收到一个对象为null的错误,这是因为在直接投射线上没有拉出datagridview。 I have multiple datagridviews on a tab control tool, one datagridview per tab.
我在一个标签页控制工具上有多个datagridviews,每个标签页一个datagridview。 Here is my code:
这是我的代码:
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
The error is on line 错误在线
Dim RelativeDGV = DirectCast(Me.Controls("DataGridLine" & value), DataGridView)
But the null error dosen't trigger until 但是直到出现空错误才触发
RelativeDGV.DataSource = dt
Try to use list of DataGridView
like this : 尝试像这样使用
DataGridView
列表:
Try
Dim array() As DataGridView = {DataGridLine2, DataGridLine3, DataGridLine4, DataGridLine7, DataGridLine8, DataGridLine10, DataGridLine11, DataGridLine12}
For Each RelativeDGV As DataGridView In array
Dim value As Integer = Regex.Replace(RelativeDGV.Name, "[^0-9]+", String.Empty)
'or like this
'Dim value As Integer = RelativeDGV.Name.Substring(12, RelativeDGV.Name.Length - 12)
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
If the various DGV controls are on other tabs, they wont be in Me.Controls
. 如果各种DGV控件在其他选项卡上,则它们将
Me.Controls
。 Rather than fish them out and cast them, you can iterate an array of them since you know the name. 因为知道名称,所以您可以迭代它们的数组,而不必将它们扔掉并抛弃它们。 You also do not need to create a new connection for each nor duplicate datatables for each:
您也不需要为每个连接创建新连接,也不需要为每个连接创建重复的数据表:
Dim dgvCtrls As DataGridView() = {DataGridLine2, DataGridLine3, DataGridLine4}
Using conn1 As New SqlConnection(connstring)
conn1.Open()
Using comm1 As New SqlCommand("SELECT LineNumber FROM...", conn1)
' ...
dt.Load(comm1.ExecuteReader())
End Using
conn1.Close()
End Using
For Each dgv In dgvCtrls
dgv.DataSource = dt
Next
You'd only need 8 identical DataTables if you dont want each grid to automatically reflect changes made in the others. 如果您不希望每个网格自动反映其他网格所做的更改,则只需要8个相同的DataTable。 For that, use a dataset on the same connection to create the tables:
为此,请在同一连接上使用数据集来创建表:
Dim SQL = "..."
Dim dgvCtrls As DataGridView() = {dgv5, dgv2, dgv3,...}
Dim ds = New DataSet
Using dbcon As New SqlConnection(SQLConnStr)
Using cmd As New SqlCommand(SQL, dbcon)
dbcon.Open()
For n As Int32 = 0 To dgvCtrls.Count - 1
ds.Load(cmd.ExecuteReader, LoadOption.OverwriteChanges, dgvCtrls(n).Name)
Next
End Using
End Using
For Each dgv In dgvCtrls
dgv.DataSource = ds.Tables(dgv.Name)
Next
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.