-
Dec 6th, 2012, 06:41 PM
#1
Thread Starter
Member
[RESOLVED] When creating multiple datagridview controls at runtime, how do you hide columns?
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:
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 col.ColumnName value does directly correspond to the columns in the database, but an error is generated immediately upon this line:
Code:
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.
Thanks
-
Dec 6th, 2012, 07:05 PM
#2
Re: When creating multiple datagridview controls at runtime, how do you hide columns?
my guess is that the column doesn't have the name you think it has.
try using the columnindex instead of the name.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 6th, 2012, 07:11 PM
#3
Re: When creating multiple datagridview controls at runtime, how do you hide columns?
No. There's a problem with you not adding columns to the DGV correctly (or at all?) At least that's what I'm assuming without the relevant code to check. Declare a New DataGridViewColumn (I think that's the right nomenclature, I'm flying blind at the moment. Assign values to it properties. Add it to the DGV. Then do the same all over again for each other column.
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Dec 6th, 2012, 07:16 PM
#4
Re: When creating multiple datagridview controls at runtime, how do you hide columns?
Originally Posted by dunfiddlin
No. There's a problem with you not adding columns to the DGV correctly (or at all?) At least that's what I'm assuming without the relevant code to check. Declare a New DataGridViewColumn (I think that's the right nomenclature, I'm flying blind at the moment. Assign values to it properties. Add it to the DGV. Then do the same all over again for each other column.
No. the OP has set the datasource for the dgv which creates the columns, but they aren't named as the OP is expecting...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 6th, 2012, 07:18 PM
#5
Thread Starter
Member
Re: When creating multiple datagridview controls at runtime, how do you hide columns?
How can I find the column index with this For...Next loop? It is not a method of col at this point...
-
Dec 6th, 2012, 07:19 PM
#6
Re: When creating multiple datagridview controls at runtime, how do you hide columns?
Originally Posted by .paul.
No. the OP has set the datasource for the dgv which creates the columns, but they aren't named as the OP is expecting...
Oh right. Missed that line. Should've gone to Specsavers!
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Dec 6th, 2012, 07:27 PM
#7
Thread Starter
Member
Re: When creating multiple datagridview controls at runtime, how do you hide columns?
Aha! I figured it out. I had to move the following line:
Code:
tabNewCheckpoint.Controls.Add(dgvNewCheckpoint)
immediately below the following line:
Code:
tabctrlTimingTable.TabPages.Add(tabNewCheckpoint)
Wow, what a struggle! Thanks for your help though!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|