Results 1 to 7 of 7

Thread: [RESOLVED] When creating multiple datagridview controls at runtime, how do you hide columns?

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2012
    Location
    Las Vegas, NV
    Posts
    41

    Resolved [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

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    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.

  3. #3
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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!

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: When creating multiple datagridview controls at runtime, how do you hide columns?

    Quote Originally Posted by dunfiddlin View Post
    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...

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2012
    Location
    Las Vegas, NV
    Posts
    41

    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...

  6. #6
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: When creating multiple datagridview controls at runtime, how do you hide columns?

    Quote Originally Posted by .paul. View Post
    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!

  7. #7

    Thread Starter
    Member
    Join Date
    Aug 2012
    Location
    Las Vegas, NV
    Posts
    41

    Resolved 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
  •  



Click Here to Expand Forum to Full Width