datagridview如何在代码中直接赋值后?

How can the datagridview directly after assign a value in code?

我有一个 WinForms 应用程序,我在表单中使用了 datagridview。 DataGridView 有很多复选框列,可以通过代码选择和处理单元格。

因此可以 check/uncheck 所有单元格或从剪贴板复制 to/insert。除了最后选中的复选框外,这工作正常。只要单元格被选中,第一个值就不会显示。

我试过设置SuspendLayoutRefreshUpdate然后ResumeLayout。但是其中任何一个都不适合我。

private void SetCheckBoxCells(bool value)
{
  myDataGridView.SuspendLayout();

  foreach (var cell in myDataGridView.SelectedCells)
  {
    (cell as DataGridViewCheckBoxCell).Value = value;
  }

  myDataGridView.Refresh();
  myDataGridView.Update();         
  myDataGridView.ResumeLayout();
}

我希望在单击菜单项后所有单元格都是可见的 set/unset,但取消选择单元格后最后一个单元格是可见的。

这个问题几乎可以肯定不是由 Refresh/Update 引起的。如果这是一个问题,您会在其他单元格中首次手动编辑后看到正确的值。您 也可以检查值 ,即:

MSgbox(myDataGridView.Rows[1].Cells[0].Value.ToString);

显然,索引正确。不过,我相信你会得到你所看到的。

foreach in SelectedCells 应该非常健壮并且不会出现索引错误,所以我会仔细检查 CellEdit 状态或 CurrentCell 焦点或者某些事件不会干扰第一个单元格,从而阻止它更新。

也许尝试将当前单元格设置为所选范围之外的内容:

myDataGridView.CurrentCell = myDataGridView.Rows[1].Cells[0];
    // some other cell not included in selection

整数(x1 到 y2)在那里做什么?他们没有工作。刚结束测试?尝试直接解决有问题的单元格(仅用于调试),就像这样:

myDataGridView.Rows[1].Cells[0].value = True

显然索引正确。也许它会更多地说明问题。