【问题标题】:Unable to check or uncheck DataGridViewCheckBoxColumn cells programmatically无法以编程方式检查或取消选中 DataGridViewCheckBoxColumn 单元格
【发布时间】:2015-09-25 08:00:08
【问题描述】:

我正在尝试通过此代码更改 DataGridViewCheckBoxColumn 的项目

        foreach (DataGridViewRow dgvUser in UsersGrid.Rows)
        {
          dgvUser.Cells["ChkSelect"].Value = true;
        }

复选框的值已更改,但 UI 上的复选框保持未选中状态。

怎么做?

【问题讨论】:

标签: c# datagridview checkbox


【解决方案1】:

你需要更新 Datagridview 然后刷新。

DataGrid_ABC.Update();为了更新gridview中的变化

【讨论】:

    【解决方案2】:

    这里的问题是DataGridViewCheckboxCell 与其他单元格的行为不同。出于某种原因,用于确定应如何绘制(选中或未选中)复选框的属性不是Value,而是FormattedValue

    我的感觉是,DataGridViewCheckBoxCell 中的 Value setter 已被覆盖以正确运行。因此,如果您在 DataGridViewCell 上调用它,则您使用的是 base (不影响 FormattedValue),而如果您投射单元格,则应该可以工作:

    ((DataGridViewCheckBoxCell)  dgvUser.Cells["ChkSelect"]).Value = true;
    

    【讨论】:

    • 这也无济于事。
    【解决方案3】:

    当我尝试清除同一行中的其他复选框列时(在 CellValueChanged 方法中执行),我遇到了这个确切的问题。为了解决我的问题,我需要以下代码:

    private void dgv_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        // filter for valid DataGridViewCheckBoxCell columns, if desired
        dgv.CommitEdit(DataGridViewDataErrorContexts.Commit);
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多