Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get index of the last selected row from a selection of rows in a DataGridView

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.

like image 535
nawfal Avatar asked Jan 19 '23 16:01

nawfal


2 Answers

if (dataGridView1.SelectedRows.Count > 0)
{
    int lastIndex = dataGridView1.SelectedRows[dataGridView1.SelectedRows.Count - 1].Index;
}
like image 101
Bala R Avatar answered Jan 22 '23 05:01

Bala R


var x = dataGridView1.SelectedRows.Cast<DataGridViewRow>().Max(row => row.Index);

is the same to:

var y = dataGridView1.SelectedRows.Cast<DataGridViewRow>().Last().Index;
like image 20
abatishchev Avatar answered Jan 22 '23 05:01

abatishchev