0
votes

I want to obtain a list of the row indices of selected cells in a DataGridView.

I was previously using SelectedCells to get this information, but this takes around 40 seconds when there are 2 million rows and all are selected.

this.SelectedCells
    .Cast<DataGridViewCell>()
     .Select(c => c.RowIndex)

On Msdn it says that SelectedCells is slow for large grids, but apart from the special case where all cells happen to be selected, does not offer many alternatives. What if all but 1 row/cell is selected?

Is there a faster method?

1
what is you question clearly?Rémi
He wants a faster alternative to SelectedCellsAseem Gautam

1 Answers

1
votes

It's a hack, but I eventually reverted to using reflection to access the private field DataGridView.individualSelectedCells, which is a linked list of the selected cells. This is much faster than accessing DataGridView.SelectedCells, though obviously less robust.