AD
Administrator
Syncfusion Team
April 1, 2005 09:47 AM UTC
If you use "grid.DataSource=dt", then the grid is really bound to dt.DefaultView. You need to use the DataView, and not the Datatable, to support things like sorting and deleting. If your grid is sorted, the DataTable is no longer mapped 1 to 1 with the rows in the grid. it is the Dataview that is mapped 1-1 with the rows in the grid.
Given a position in the DataView (or more generally in the CurrencyManager.List), you can get the rowIndex in the grid using grid.Binder.PositionToRowIndex method.
Given a rowIndex in the grid, you can use grid.Binder.RowIndexToPosition to get the position in the DataView or (CurrencyManager.List).
For example, to get the DataRow object from your DataTable associated with the grid row at rowIndex, use code like:
int position = grid.Binder.RowIndexToPosition(rowIndex);
CurrencyManager cm = grid.BindingContext[grid.DataSource, grid.DataMember] as CurrencyManager;
DataView dv = cm.List as DataView;
DataRowView drv = dv[position];
DataRow dr = drv.Row;
Now if you want to find a grid rowIndex from some arbitary position in the DataTable, you muts first find where this arbitary position is in the associated DataView. If things are sorted, you can use DataView.Find or soemthing similar. If they are not, then you may have to loop through the dataview to locate the position. Once you have the position in the DataView, you can use grid.Binder.PositionToRowIndex to get teh grid row.