We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

DBG - how to find DataRow after sorting?

I posted this yesterday and thought Haneef's solution was working but I was mistaken. Here's the issue:

I am handling the PrepareViewStyleInfo to do some custom formatting in a data bound grid (DBG). This formatting depends on data in bound datasource's row corresponding to the row in the event args e.RowIndex value.

I use Binder.RowIndexToPosition (and even tried Binder.RowIndexToListManagerPosition). Using this value gives me an index into the DataTable that works ONLY IF I HAVEN"T SORTED THE GRID. Once the grid is sorted, this value is not the correct.

My question is: Given a row index in a (potentially sorted and/or filtered) DBG, how can I retrieve row in the DataTable that is bound to the grid? There must be some way, because the grid knows how to get the correct data for painting.


3 Replies

AD Administrator Syncfusion Team June 14, 2007 01:16 PM UTC

The position returned by Binder.RowIndexToPosition should be used in DataTable.DefaultView to retrieve the DataRowView associated with the grid row. (Using it as an index into the DataTable itself will not necessarily return the proper DataRow as you noted.)


DW Dave Wilkins June 14, 2007 01:38 PM UTC

This still leads to the row that was originally in that position before the sort.

I'm doing
int dataIndex = this.gridDataBoundOrders.Binder.RowIndexToPosition(e.RowIndex);
rowTest = this.ordersDataSet.Orders.DefaultView[dataIndex].Row as OrdersDataSet.OrdersRow;

But the rowTest is not the correct row after sorting. Am I missing something? BTW, the "sorting" is done through the BDG by double clicking the col header. The grid is showing the correctly sorted rows, but the DefaultView row ordering is not changed.

>The position returned by Binder.RowIndexToPosition should be used in DataTable.DefaultView to retrieve the DataRowView associated with the grid row. (Using it as an index into the DataTable itself will not necessarily return the proper DataRow as you noted.)


AD Administrator Syncfusion Team June 14, 2007 03:24 PM UTC

If you are trying to find a row after a sort just by knowing its rownumber before the sort, then this will not work. Instead of just saving the rownumber, you need to save enough information to uniquely ID the row after the sort. One normal way of doing this is to use primary keys. This way, you can ID a row by checking its primary keys.

Below is a sample that allows you to click a button and set the backcolor of the current row using PrepareViewStyleInfo. It does this by saving the primary key of the colored row and looking for this key in PrepareViewStyleInfo to apply the color. To get the primary key of the current row, the button handler uses the code suggested above. This technique works even after the grid is sorted many times.

ColorRows.zip

Loader.
Live Chat Icon For mobile
Up arrow icon