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

```