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:
The col.ColumnName value does directly correspond to the columns in the database, but an error is generated immediately upon this line:Code: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 error that I keep getting is: "Object reference not set to an instance of an object"Code:dgvNewCheckpoint.Columns(col.ColumnName).Visible = False
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.
Thanks




Reply With Quote
