繁体   English   中英

直接强制转换为datagridview提供空错误

[英]Direct Cast giving null error for datagridview

我目前正在2013年面向台式机的VB.NET Express中工作。我很难将SQL数据绑定到具有数组的循环上的某些datagridviews。 我收到一个对象为null的错误,这是因为在直接投射线上没有拉出datagridview。 我在一个标签页控制工具上有多个datagridviews,每个标签页一个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

尝试像这样使用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

如果各种DGV控件在其他选项卡上,则它们将Me.Controls 因为知道名称,所以您可以迭代它们的数组,而不必将它们扔掉并抛弃它们。 您也不需要为每个连接创建新连接,也不需要为每个连接创建重复的数据表:

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

如果您不希望每个网格自动反映其他网格所做的更改,则只需要8个相同的DataTable。 为此,请在同一连接上使用数据集来创建表:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM