I don''t know how to map datarow index and grid index in GridDataBoundGrid.
- I have a datatable which have 10 rows.
- Using method "grid.DataSource=dt" to bind data.
- after that, I used "DeleteRecordsAtRowIndex" to delete 5 rows. And now, in the grid, there are 5 rows
- And in datatable still exists 10 rows( 5 rows is marked as Deleted and 5 rows is marked as Unchanged)
Please let me know how to get the position in the grid if I know the row index in datatable.
ADAdministrator Syncfusion Team April 1, 2005 04:47 AM
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.