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

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
 
C

Cor Ligthert

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top