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.
Unfortunately, activation email could not send to your email. Please try again.

DBG - how to find DataRow after sorting?

Thread ID:

Created:

Updated:

Platform:

Replies:

62409 Jun 14,2007 07:57 AM Jun 14,2007 11:24 AM Windows Forms 3
loading
Tags: GridControl
Dave Wilkins
Asked On June 14, 2007 07:57 AM

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.


Administrator [Syncfusion]
Replied On June 14, 2007 09:16 AM

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.)

Dave Wilkins
Replied On June 14, 2007 09:38 AM

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.)

Administrator [Syncfusion]
Replied On June 14, 2007 11:24 AM

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

CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;