0
votes

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.

2

2 Answers

0
votes

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

0
votes

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

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