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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reference datagrid cell in selected row by column name

Status
Not open for further replies.

jennyflower

Programmer
Oct 10, 2006
60
GB
Hi, I have a datagrid with a load of data in it and I want to know what is in the say order_number column of a selected row. I know I can do this through DataGrid1.Item(row_no, col_no) but I dont want to have to use the number as the grids will be changeable for each user.

Is there a way to do this using the column name somehow?

I am using visual studio 2003 with vb.net

Thanks
 
Hi,

I haven't used datagrids for a long time but I think something like this should work (it assumes that you have applied tablestyle to your datagrid) :

Code:
Public Function GetColumnIndex(ColumnName as string) as integer

  Dim yourColumnIndex As Integer
  yourColumnIndex = DataGrid1.TableStyles("SourceName").GridColumnStyles. _
                          IndexOf(DataGrid1.TableStyles("SourceName").GridColumnStyles.Item(ColumnName))

  return yourColumnIndex
End Function


'Somewhere else in your code
DataGrid1.Item(row_no, GetColumnIndex("NameOfYourColumn"))

I hope this little improvised work around will help !
Cheers

---
[tt][COLOR=white Black]MASTA[/color][COLOR=Black lightgrey]KILLA[/color][/tt] [pipe]
 
Thanks, Ill give it a go. Just a question though, what do you use instead of a datagrid?
 
Hi,

when I started with vs2003, I was using Datagrid, then when I moved to vs2005 I used DataGridView, wich are much better in my opinion. Now I use third party components like the XtraGrid developped by Developer Express. They have great components and a very good support team.

Cheers!

---
[tt][COLOR=white Black]MASTA[/color][COLOR=Black lightgrey]KILLA[/color][/tt] [pipe]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top