I have a DataGridView and when I select multiple rows, I want the index of the last selected row. In other words, how to get the maximum most index from a selection of rows.
e.g., if I select row0, row1 and row6, I want the output as "6".
Regards.
if (dataGridView1.SelectedRows.Count > 0)
{
int lastIndex = dataGridView1.SelectedRows[dataGridView1.SelectedRows.Count - 1].Index;
}
var x = dataGridView1.SelectedRows.Cast<DataGridViewRow>().Max(row => row.Index);
is the same to:
var y = dataGridView1.SelectedRows.Cast<DataGridViewRow>().Last().Index;
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