在DataGridView.SelectedCells细胞订单?细胞、订单、DataGridView、SelectedCells

由网友(倔强小王子)分享简介:我想知道什么是细胞DataGridView.SelectedCells的顺序?我假设了selectedCells [0]将返回第一个单元格(顶部最左边的)和了selectedCells [计数-1]的最后一个(底部最右边的),但奇怪的是有以下四个单元格[1] [2] [3] [4] 当我选择这些细胞,并尝试获取单元格...

我想知道什么是细胞DataGridView.SelectedCells的顺序?我假设了selectedCells [0]将返回第一个单元格(顶部最左边的)和了selectedCells [计数-1]的最后一个(底部最右边的),但奇怪的是有以下四个单元格

[1] [2] [3] [4]

当我选择这些细胞,并尝试获取单元格值与以下code

 的for(int i = 0; I< gridView.SelectedCells.Count;我++)
    的MessageBox.show(gridView.SelectedCells [I] .Value.ToString());
 

它会显示在错误的顺序值低于

4 2 3 1

我缺少的东西?有DataGridView中的某些属性,指定所选单元格的顺序?请注意,我通过从左上角-大多数细胞拖动光标(具有值1)的底部,最右边的细胞(具有值4)选择的细胞

解决方案

  VAR下令= gridView.SelectedCells.OrderBy(C => c.Index)
 

一点点的LINQ to救援:-)这将升序排列细胞的指标进行排序。

卫星胶质细胞 星形胶质细胞和小胶质细胞在奥沙利铂诱导的神经性疼痛中的作用

I am wondering what is the order of the cells in DataGridView.SelectedCells? I assumed SelectedCells[0] shall return the first cell (top-left-most) and SelectedCells[Count-1] the last (bottom-right-most) but strangely enough with the following four cell grid

[ 1 ] [ 2 ] [ 3 ] [ 4 ]

when I select these cells and try to get cell values with the following code

for (int i = 0; i < gridView.SelectedCells.Count; i++)
    MessageBox.Show(gridView.SelectedCells[i].Value.ToString());

it displays values in wrong order as below

4 2 3 1

Am I missing something? Is there some property of DataGridView that specifies the ordering of selected cells? Note that I select cells by dragging cursor from top-left-most cell (with value 1) to the bottom-right-most cell (with value 4).

解决方案

var ordered = gridView.SelectedCells.OrderBy(c=>c.Index)

a little LINQ to the rescue :-) This will sort your cells by Index in ascending order.

阅读全文

相关推荐

最新文章