1

我有一组选定的单元格。由于有多个列,集合中单元格的行索引通常采用 2、2、2、3、3、3、4、4、4 等格式。

我想获得这些单元格的行索引列表。

List<int> selectedRows = new List<int>();

List<DataGridViewCell> cellCollection = dGV_model.SelectedCells.Cast<DataGridViewCell>()
                                      .GroupBy(cell => cell.RowIndex)
                                      .Select(cell => cell.First())
                                      .ToList<DataGridViewCell>();
foreach (DataGridViewCell cell in cellCollection)
{
    selectedRows.Add(cell.RowIndex);
}

本质上,我的问题是如何从单个 LINQ 查询创建一个 int 列表?现在,我必须遍历 cellcollection 以将它们添加到 int 列表中。

4

2 回答 2

5
var selectedRows = dGV_model.SelectedCells.Cast<DataGridViewCell>()
                                          .Select(c=>c.RowIndex).Distinct()
                                          .ToList();
于 2013-10-11T08:28:13.750 回答
0

这应该做你想要的:

var rows = dGV_model.SelectedCells.Cast<DataGridViewCell>()
                            .Select(x=>x.RowIndex).Distinct()
                            .ToList();
于 2013-10-11T08:31:08.950 回答