Serge
Serge

Reputation: 653

Iterate through selected gridview cells by row index

I am using a GridView control to display some data and I need to programatically change the values of the selected cells. When I iterate through the selected cells collection, it is sorted in the order which you selected the cells in. For example, the row indexes may be 1, 1, 1, 2, 2, 2, 1, 2, 1, 2. I want to edit all the cells with row index 1 before I move on to 2.

Upvotes: 1

Views: 1585

Answers (2)

MGZero
MGZero

Reputation: 5983

Why not just for each over the rows, and have an inner for each loop to go through the columns?

Upvotes: 0

Luc Morin
Luc Morin

Reputation: 5380

If I understand you correctly, you want to iterate through the SelectedCells collection of a Windows Forms DataGridView in Row Index order.

I don't have a real setup to test this right now, but you could try something like this:

var q = dataGridView1.SelectedCells.OfType<DataGridViewCell>().OrderBy(x => x.RowIndex);

You can then "foreach" over q

Cheers

Upvotes: 2

Related Questions