0

我目前正在使用 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
4

2 回答 2

1

尝试使用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
于 2016-06-13T14:44:28.223 回答
1

如果各种 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 个相同的数据表。为此,请使用同一连接上的数据集来创建表:

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
于 2016-06-13T15:00:42.993 回答