Grid.Item does not accept column name for second argument.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need to dynamically retrieve the column index of a Windows Forms datagrid column. To reference the column I used the following code

Me.GridAddresses.TableStyles(0).GridColumnStyles.Item("StreetName"

Unfortunately, the column itself does not have an indexof method or any property that refers to its index. I need to populate a Datagrid Column with a value despite its location in the grid. As of now I am using this code

Grid.Item(i, 3) = dr.Item(1

As you can see I am hardcoding the column index (3). The Grid.Item only allows you to use the row number and column number or a reference to a DatagridCell object. Unfortunately, the DatagridCell object does not have a constructor where you can use the column name instead of column number

Does anyone know of a way around this? Any help would be greatly appreciated

Manch
 
Hi Manch,

You means something as this

Dim col As Integer
Dim found As Boolean
For col = 0 To DirectCast _
(DataGrid1.TableStyles(0).GridColumnStyles, IList).Count - 1
If DataGrid1.TableStyles(0).GridColumnStyles(col).MappingName _
= "StreetName" Then
found = True
Exit For
End If
Next
If found Then
DataGrid1.Item(DataGrid1.CurrentCell.RowNumber, col) = dr.Item(1)
End If

This was the shortest I could find so fast.
:-)

I hope this works for you?

Cor
 
Back
Top