[DataGridView/cols] Use name instead of number?

littlebigman

Well-known member
Joined
Jan 5, 2010
Messages
75
Programming Experience
Beginner
Hello

I'm beginning to learn VB 2008 with the Express Edition IDE, and have a question about using the DataGridView control.

I'd like to be able to refer to columns by their name instead of their index:

VB.NET:
With DataGridView1
    .ReadOnly = True
    .RowHeadersVisible = False

    .ColumnCount = 2
    .Columns(0).HeaderText = "My File"
    .Columns(0).Name = "File"
    .Columns(1).HeaderText = "First Line"
    .Columns(1).Name = "Data"

    '.Item("Key","Value").Value = "test"
    .Item(0, 0).Value = "test"
End With

Is this possible or are we required to always use indexes?

Thank you.
 
Apparently, the recommended way is not to use Item(), but rather Rows.Add():

VB.NET:
With DataGridView1
  'Remove empty row after adding row
  .AllowUserToAddRows = False
  .Rows.Add("item1", "item2")
End With

HTH,
 
I believe you can refer to a specific column in the row by name using something similar to this:
VB.NET:
If DGV.Rows(Index).ColumnName = "Name of Column" Then

End If
 
Back
Top