【问题标题】:Datagridview checkbox checked when clicking the cell单击单元格时选中 Datagridview 复选框
【发布时间】:2015-06-06 13:56:47
【问题描述】:

我使用 CurrentCellDirtyStateChanged 处理我的复选框单击事件。当我单击包含复选框的 cell 时,我想要做的是处理相同的事件,即当我单击单元格时,选中复选框并调用 DirtyStateChanged。使用下面的代码没有多大帮助,它甚至没有调用 CurrentCellDirtyStateChanged。我的想法已经用完了。

private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if(dataGridView.Columns[e.ColumnIndex].ReadOnly != true)
    {       
          //option 1
          (dataGridView.CurrentRow.Cells[e.ColumnIndex] as DataGridViewCheckBoxCell).Value = true;
          //option 2
          DataGridViewCheckBoxCell cbc = (dataGridView.CurrentRow.Cells[e.ColumnIndex] as DataGridViewCheckBoxCell);
          cbc.Value = true;
          //option 3
          dataGridView.CurrentCell.Value = true;
    }

}

【问题讨论】:

  • 这是 XAML 吗?这将是比 [cell] 和 [checked] 更好的问题标签
  • 为什么这个问题会得到减分?
  • 投反对票是因为 SO 有很多固执己见的白痴认为这是增加价值的最佳方式。
  • 我同意,这些白痴应该给出一个更好的解决方案,或者,至少是一个合理的理由以及赞成/反对票,而不是有偏见。

标签: c# winforms checkbox datagridview checked


【解决方案1】:

这应该做你想要的:

private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if(dataGridView.Columns[e.ColumnIndex].ReadOnly != true)
    {       
        dataGridView.CurrentCell.Value = true;
        dataGridView.NotifyCurrentCellDirty(true);
    }
}

【讨论】:

    【解决方案2】:

    正如 Bioukh 指出的,您必须调用 NotifyCurrentCellDirty(true) 来触发您的事件处理程序。但是,添加该行将不再更新您的选中状态。为了完成您检查的状态更改点击,我们将添加对RefreshEdit 的调用。这将在单击单元格时切换您的单元格检查状态,但它也会使实际复选框的第一次单击有点错误。所以我们添加了CellContentClick 事件处理程序,如下所示,您应该一切顺利。


    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
      DataGridViewCheckBoxCell cell = this.dataGridView1.CurrentCell as DataGridViewCheckBoxCell;
    
      if (cell != null && !cell.ReadOnly)
      {
        cell.Value = cell.Value == null || !((bool)cell.Value);
        this.dataGridView1.RefreshEdit();
        this.dataGridView1.NotifyCurrentCellDirty(true);
      }
    }
    

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
      this.dataGridView1.RefreshEdit();
    }
    

    【讨论】:

    • 感谢您的回复。我得到“指定的演员表无效”异常:(bool)cell.value
    • @Jnr 如果不是TrueFalse,单元格的值是多少?
    • 这是一个很好的问题,我没有提到 datagridview 是数据绑定的。所以值为 DBNull。我没有使用强制转换为 bool,而是使用了 dataGridView.CurrentCell.Value = true;感谢您的帮助!
    • 很高兴为您提供帮助!很好地解决了这个问题。
    • 效果很好。您可以摆脱“cell.Value == null ||”因为它保证在那一行不为空。
    【解决方案3】:
    private void dataGridView3_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == dataGridView3.Columns["Select"].Index)//checking for select click
            {
                dataGridView3.CurrentCell.Value = dataGridView3.CurrentCell.FormattedValue.ToString() == "True" ? false : true;
                dataGridView3.RefreshEdit();
            }
        }
    

    这些更改对我有用!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-27
      • 1970-01-01
      相关资源
      最近更新 更多