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 540447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:14:28+00:00 2026-05-13T10:14:28+00:00

This is something that has been bugging me for a while as it is

  • 0

This is something that has been bugging me for a while as it is easily fixed but not desirable.

I have a DataGridView that has 5 columns. The first is called ID.

In vb.net the following line gives an error “Object reference not set to an instance of an object”:

dgvJobs.Columns("ID").Visible = False ' ERROR
dgvJobs.Columns(0).Visible = False ' OK

Obviously using the name is much better than a hard coded value to reference the column but wondering if there is anything i can do to get this to work correctly?

The datagridview datasource is BindingSource control with the datasource being a dataset.

EDIT: Based on the answer I have created the following function that does exactly as I need:

Private Function GetColName(ByVal name As String, ByRef dgv As DataGridView) As Integer
    Dim retVal As Integer

    For Each col As DataGridViewColumn In dgv.Columns
        If col.HeaderText = name Then
            retVal = col.Index
            Exit For
        End If
    Next

    Return retVal

End Function

Useage:

dgvJobs.Columns(GetColName("ID", dgvJobs)).Visible = False
  • 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-05-13T10:14:28+00:00Added an answer on May 13, 2026 at 10:14 am

    Two tips:

    1- By default, the Winforms designer creates an object in your forms Class for every column that exists in a DataGridView. So if you have a column named MyIdColumn, for instance, you should be able to access the column in the form code like this:

    Me.MyIdColumn.Visible = False
    

    If you want to access an element of the DataGridView, you could do something like this:

    Dim value = dgv.Item(MyIdColumn.Index, rowIndex).Value
    

    Using this trick, if the column name changes, there’s no problem because you reference the column directly instead of a string representing a column name that could change.

    2- Your GetColName method could be better; I would transform it into an extension method:

    <System.Runtime.CompilerServices.Extension()> _
    Private Function GetColByHeaderText(ByVal dgv As DataGridView, ByVal name As String) As DataGridViewColumn
    
        For Each col As DataGridViewColumn In dgv.Columns
            If col.HeaderText = name Then
                Return col
            End If
        Next
    
        Return Nothing
    
    End Function
    

    Usage:

    dgv.GetColByHeaderText("ID").Visible = False
    

    A lot nicer IMO!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 335k
  • Answers 335k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Take a look at: list<KeySlice> get_range_slices(keyspace, column_parent, predicate, range, consistency_level)… May 14, 2026 at 3:39 am
  • Editorial Team
    Editorial Team added an answer Simple, really: instead of a simple counter for each 'bucket',… May 14, 2026 at 3:39 am
  • Editorial Team
    Editorial Team added an answer Found a way to get the results I want: FOR… May 14, 2026 at 3:39 am

Related Questions

This is one thing that has been bugging me for a while about DDD.
This is a question that has been bugging me for a while and I
I would like to register an HttpHandler to include all subfolders of a root
This problem has been bugging me for a while. I have to load a
This is really bugging me. I'm using a GridView and want to format it

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.