Retrieving data table index for datagrid index selected

  • Thread starter Thread starter Dvae c
  • Start date Start date
D

Dvae c

I defined the source of a datagrid to a datatable. Because the
datagrid is actually bound to the default dataview, the data index
selected in the datagrid might not equal the actual datatable index
(i.e. if datagrid is sorted, filter applied).
Is there a way I can translate the datagrid selected index into the
proper datatable index value so as to get the correct data table row?
 
VB.NET
Dim row as DataRow = CType(dataGrid.DataSource,
DataView)(dataGrid.CurrentRowIndex).Row
C#
DataRow row = (dataGrid.DataSource as
DataView)[dataGrid.CurrentRowIndex].Row as DataRow;
 
I use HitTestInfo class of DataGrid to get the row selected, column
selected, or cell selected.
Here is an example of row selection
(I usually use OnMouseUp event)
Dim hti as DataGrid.HitTestInfo = myDataGrid.HitTest(e.X, e.Y)
Dim intRow as Integer
If hti.Type = DataGrid.HitTestType.Cell Then
intRow = hti.Row
End If

Hope this helps...
Paul
 
Back
Top