Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help With Windows Forms DatagridColumn

Status
Not open for further replies.

Manch

Programmer
Jun 12, 2001
43
0
0
US
I need to dynamically retrieve the column index of a Windows Forms datagrid column. To reference the column I used the following code:
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:
Code:
Grid.Item(i, 3) = dr.Item(1)
As you can see I am hardcoding the column index. 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
 
Manch,

Try using this expression

Code:
Dim sColumnName as string

sColumnName = Grid.Rows(Grid.CurrentCell.RowNumber).Table.Columns (Grid.CurrentCell.ColumnNumber).ColumnName

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

This will give you the column name that you are trying to access in datagrid. I think that should be enough to avoid the hard-coding.

-Kris
 
Thanks for the reply Kris11!

Unfortunately, Grid.Item does not accept a column name (string) as its second argument. It only allows an index(integer). If you know of another way, of if I am incorrect, please let me know. I do appreciate your reply.

Manch.
 
Manch,

I am sorry I didn't test it before posting, actually I think this is what you are looking for

Code:
With datagrid1.TableStyles(0)

    .GridColumnStyles.IndexOf (.GridColumnStyles(<Column_Name>))
End With

This will return you an index which represents the position of column in datagrid. Let me know if this worked or not.

-Kris
 
Kris11,

I have already tried that. I also thought it would work, but GridColumnStyles.IndexOf actually gives you the index of a GridColumnStyle object within the GridColumnStyles collection. A GridColumnStyle object holds the actual DataGridColumns.

Thanks again for trying. I will let you know if I find a solution.

Manch
 
Kris11,

In case you are interested, I did find a solution. Since I already know the column name I want and just need its index, I looped through the ColumnStyles and checked the mapping name for each column. When I find the column I am looking for, exit the loop. The value of my looping integer is my index:

Code:
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

Thanks again for your replies.
Phil
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top