This is something that bothered me for a while, as it is easily fixed, but not desirable.
I have a DataGridView with 5 columns. The first is called an identifier.
In vb.net, the following line gives the error "Object reference not installed on object instance":
dgvJobs.Columns("ID").Visible = False ' ERROR
dgvJobs.Columns(0).Visible = False ' OK
Obviously, using a name is much better than a hard-coded value for a column reference, but I wonder if there is anything I can do to make this work correctly?
This datagridview data source is a BindingSource control with a data source as a dataset.
EDIT: Based on the answer, I created the following function that works exactly the way 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