Vij*_*ade 4 c# datagridview selection winforms
我在Windows窗体上有一个DatagridView控件.它的selectionMode属性设置为CellSelect.
我想基于选定的单元格操作DatagridViewRow.DataGridView控件绑定到DataSource.
如何根据选定的单元格获取Row集合?
小智 7
Linq提供的答案与提供的语法不兼容.Datagridview不支持可数字,因此您必须使用:
IEnumerable<DataGridViewRow> selectedRows = dgPOPLines.SelectedCells.Cast<DataGridViewCell>()
.Select(cell => cell.OwningRow)
.Distinct();
Run Code Online (Sandbox Code Playgroud)
DataGridView.SelectedCells将为您提供所选单元格的列表.DataGridViewCell该集合中的每个实例都有一个OwningRow,这允许您构建自己的行集合.
例如:
using System.Linq;
IEnumerable<DataGridViewRow> selectedRows = dgv.SelectedCells
.Select(cell => cell.OwningRow)
.Distinct();
Run Code Online (Sandbox Code Playgroud)
List<DataGridViewRow> rowCollection = new List<DataGridViewRow>();
foreach(DataGridViewCell cell in dataGridView.SelectedCells)
{
rowCollection.Add(dataGridView.Rows[cell.RowIndex];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12530 次 |
| 最近记录: |