在DataGridView.SelectedCells细胞订单? [英] Cell order in DataGridView.SelectedCells?

查看:400
本文介绍了在DataGridView.SelectedCells细胞订单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道什么是细胞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.

这篇关于在DataGridView.SelectedCells细胞订单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆