【问题标题】:Programmatically multiselect rows in WinForms DatagridView properly以编程方式正确选择 WinForms DatagridView 中的行
【发布时间】:2011-07-26 09:51:05
【问题描述】:

是否可以,如果可以,如何以编程方式选择标准 WinForms 数据网格中的非连续行?

foreach (var selectableRowIndex in selectableRowIndices)
{
    dataGridView.Rows[selectableRowIndex ].Selected = true;
}

... 确实有效,但是在通过设置 dataGridView 的 .CurrentCell 属性之后

dataGridView.CurrentCell = dataGridView.Rows[underlyingRowIndex].Cells[1];

所有其他行都被取消选择。

设置当前行/单元格并保持选中其他行的正确方法是什么?

【问题讨论】:

  • 既然您在谈论Rows 属性,我假设您使用的是DataGridView 而不是DataGrid 类。您使用的代码在这里可以正常工作...您能否确认您的DataGridViewMultiSelect 属性设置为true
  • Multiselect 确实设置为 true,是的,它是一个 DataGridView,很抱歉造成混淆。

标签: .net winforms datagrid


【解决方案1】:

我遇到了同样的问题,我的解决方案是使用带有所选行 ID 的 List<Int32>,并根据需要添加或删除 ID。

【讨论】:

    【解决方案2】:

    一个快速而肮脏的解决方案是:

    -获取当前选中行的ID
    -更改当前单元格
    -重新选择行

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-10
      • 2019-04-10
      • 1970-01-01
      • 2011-04-30
      • 1970-01-01
      • 1970-01-01
      • 2021-03-08
      相关资源
      最近更新 更多