Voi*_*ark 2 c# forms datagridview selected
所以我有这个DataGridView
而这段代码:
if(dataGridView1.SelectedRows.Count > 1)
{
MessageBox.Show("Error: More than one value selected");
return false;
}
Run Code Online (Sandbox Code Playgroud)
如果我有 2 个完全选择的行,则正确计数为 2。但我想检查是否选择了来自 2 个不同行或更多行的任何单元格。
换句话说: 在我的图片中,我当前的选择目前返回 1,但我希望它返回 2。
谢谢你。
修复后编辑:工作代码:
if(dataGridView1.SelectedCells.Cast<DataGridViewCell>().Select(c => c.RowIndex).Distinct().Count() > 1)
{
MessageBox.Show("Error: More than one value selected");
return false;
}
Run Code Online (Sandbox Code Playgroud)
要获取所选单元格的行数:
int count = dataGridView1.SelectedCells.Cast<DataGridViewCell>()
.Select(c => c.RowIndex).Distinct().Count();
Run Code Online (Sandbox Code Playgroud)
要检查是否选择了多行:
var selectedCells = dataGridView1.SelectedCells;
bool check = selectedCells.Count > 0
&& selectedCells.Cast<DataGridViewCell>().Any(c => c.RowIndex != selectedCells[0].RowIndex);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1323 次 |
| 最近记录: |