开发者

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.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜