如果只选择部分行,如何获取所有选定的行?

时间:2013-02-25 22:53:13

标签: c# winforms datagridview

如何选择选择某些内容的所有行?假设我们在不同/相同的列中选择了10个项目,如下所示:

enter image description here

我们希望获得类似于

的结果
var selected = ElementsTableView.SelectedRows.Cast<DataGridViewRow>();

但是对于其中至少选择了一个列项的所有行。如何用C#中的WinForms做这样的事情?

2 个答案:

答案 0 :(得分:11)

试试这个:

var selected = ElementsTableView
               .SelectedCells
               .Cast<DataGridViewCell>()
               .Select(c => c.OwningRow)
               .Distinct();

答案 1 :(得分:1)

DataGridView具有SelectedCells属性。 DataGridViewCell有一个OwningRow属性,可返回DataGridViewRow

请参阅MSDN文档herehere

相关问题