Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8955405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:32:52+00:00 2026-06-15T14:32:52+00:00

I am having some trouble with hiding columns in a datagridview control that I

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:32:53+00:00Added an answer on June 15, 2026 at 2:32 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble hiding the contents of some divs in a pseudo-tab set up
I´m having some trouble retrieving a collection of strings in a projection: say that
Having some trouble looping through these arrays , i have 4 properties that identical
Having some trouble with the CSS alignment of text generated with @font-face. For some
Having some trouble trying to put together a page where images are placed properly.
having some trouble with a Thread (CanvasThread) that is intermittently pausing at random points
I am having trouble with some JavaScript running before the page is completely rendered
Having some trouble getting this right, I have a list of transaction that I
Im having some trouble reading xml files using jquery that containts swedish chars (åäö)
having some trouble with my razor syntax gives a Parsor error saying that The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.