Question

I am having some trouble with hiding columns in a datagridview control that I am generating at runtime. I am generating that control inside of a runtime generated tab page in an existing tab control. My code is as follows:

Try
    Do
        If m_DataTable.Columns.Contains("Checkpoint " & intCheckPointNumber & " Time") Then
            Dim tabNewCheckpoint As New TabPage
            Dim dgvNewCheckpoint As New DataGridView

            tabNewCheckpoint.Name = "tabCheckpoint" & intCheckPointNumber
            tabNewCheckpoint.Text = "Checkpoint " & intCheckPointNumber
            tabctrlTimingTable.TabPages.Add(tabNewCheckpoint)

            dgvNewCheckpoint.Name = "dgvCheckpoint" & intCheckPointNumber
            dgvNewCheckpoint.DataSource = m_DataTable
            dgvNewCheckpoint.Size = dgvTimingP2P.Size
            tabNewCheckpoint.Controls.Add(dgvNewCheckpoint)

            Try
                strColumnName = "Checkpoint " & intCheckPointNumber
                For Each col As DataColumn In m_DataTable.Columns
                    MessageBox.Show(col.ColumnName)
                    If col.ColumnName.StartsWith("Checkpoint") Then
                        If Not col.ColumnName.StartsWith(strColumnName) Then
                            dgvNewCheckpoint.Columns(col.ColumnName).Visible = False
                        End If
                    End If
                Next
            Catch
                MessageBox.Show(ErrorToString)
            End Try
        Else
            Exit Do
        End If
        intCheckPointNumber += 1
    Loop
Catch ex As Exception
    'MessageBox.Show(ErrorToString)
End Try

The col.ColumnName value does directly correspond to the columns in the database, but an error is generated immediately upon this line:

dgvNewCheckpoint.Columns(col.ColumnName).Visible = False

The error that I keep getting is: "Object reference not set to an instance of an object"

This code works fine for a datagridview control that I have inside of another tab page in the tab control that were all created at design time.

Is there a problem with trying to hide the columns immediately after I have created the runtime generated datagridview?

I am using VB2010.

I did originally use the following code:

For Each col As DataGridViewColumn In dgvNewCheckpoint.Columns
MessageBox.Show(col.Name)
    If col.Name.StartsWith("Checkpoint") Then
        If Not col.Name.StartsWith(strColumnName) Then
            col.Visible = False
        End If
    End If
Next

The problem that I have here, is that the program skips right over this For...Next loop because it must not be seeing DataGridViewColumns in the dgvNewCheckpoint datagridview control. When I changed it to reference the columns in the database, I actually got results. Am I referencing the columns incorrectly here?

Était-ce utile?

La solution

The DGV should be added to control list before bond to a datatable. Thus the line should be moved as below:

Dim tabNewCheckpoint As New TabPage
Dim dgvNewCheckpoint As New DataGridView

tabNewCheckpoint.Name = "tabCheckpoint" & intCheckPointNumber
tabNewCheckpoint.Text = "Checkpoint " & intCheckPointNumber
tabctrlTimingTable.TabPages.Add(tabNewCheckpoint)

tabNewCheckpoint.Controls.Add(dgvNewCheckpoint) 'This is the line to be moved

dgvNewCheckpoint.Name = "dgvCheckpoint" & intCheckPointNumber
dgvNewCheckpoint.DataSource = m_DataTable
dgvNewCheckpoint.Size = dgvTimingP2P.Size
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top