arraysvb.netdatagridviewdirectcast

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. I am receiving an object null error and its because on the direct cast line its not pulling the datagridview. I have multiple datagridviews on a tab control tool, one datagridview per tab. 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

Solution

  • Try to use list of DataGridView like this :

    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