views:

95

answers:

2

Hi All,

I have a DataTable which is bound to datagridview (Winforms)... I use the following two lines to get the DataRow that is selected in the datagridview...

        int l_intSelectedRow = DataGridView1.SelectedRows[0].Index;

        DataRow l_drwSelectedRow = ControlGroupPostedItems.Tables["PostedItems"].Rows[l_intSelectedRow];

This works fine until the DataGridView is Sorted... When the gridview is sorted by the, I get the incorrect values (1st selection of the sorted view return 1st row of unsorted Table).

How could I solve this... Is looping through the datatable the only way...

Thank you.

A: 

You'll need to use a unique ID on your data objects instead of their dataset index, and look things up using that.

Matti Virkkunen
A: 

Try directly sorting the DataTable, by using the Select method.

http://msdn.microsoft.com/en-us/library/way3dy9w.aspx

thelost
How will I know, Which Column the user has sorted and in which order?
The King
In order to detect the column, you could check out where the user clicked: on which column header. For the ordering, you can keep track for each column by switching ASC/DESC on each click.
thelost