I have a DataGridView
with unbound data that contains three different DataColumns
. The rows can be sorted by each column, but other than that no manipulation of the displayed data is allowed.
When I query the SelectedRows
property the rows are sorted in the order I initially inserted them, and not like I expected in the currently displayed or selected order. Is there a way to change this behavior?
I have same problem. Its look the shortest way:
List<DataGridViewRow> rows =
(from DataGridViewRow row in dgv.SelectedRows
where !row.IsNewRow
orderby row.Index
select row).ToList<DataGridViewRow>();
The SelectedRows property contains the selected rows but in the reverse order and the most recent item is at the start of the list. To get the correct user selected order do the following code:
List<DataGridViewRow> dgList = new List<DataGridViewRow>();
foreach (DataGridViewRow r in dgv.SelectedRows)
{
dgList.Insert(0, r);
}
foreach(DataGridViewRow r in dgList)
{
//Print/consume your row here.
int selectedIndex = r.Index;
}
Note: No need to sort.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With