【问题标题】:DataGridView run method when clicking and unclicking a checkbox cell单击和取消单击复选框单元格时的 DataGridView 运行方法
【发布时间】:2017-06-22 13:54:25
【问题描述】:

这似乎是一些非常基本的功能,但我在 StackOverflow 或文档中找不到任何具体示例。

检查 DataGridView 的事件似乎没有任何可用的东西可以直接监视复选框单元格中的更改。

谁能提供一个监控datagridview中的复选框检查事件然后执行方法的示例?

【问题讨论】:

    标签: c# winforms events datagridview


    【解决方案1】:

    经过一番搜索后,我发现获取更改值的最佳方法是检查 CurrentCellDirtyStateChanged 并触发编辑并检查单元格当前值:

    private void DataGrid_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        if (DataGrid.Columns[e.ColumnIndex].Name == "colReserved")
        {
    
            DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)DataGrid.Rows[e.RowIndex].Cells["colReserved"];
    
            if ((Boolean)checkCell.Value)
            {
                //Checked
                MessageBox.Show("Checked");
            }
            else
            {
                //Not Checked
                MessageBox.Show("UnChecked");
            }
    
            DataGrid.Invalidate();
        }
    }
    
    private void DataGrid_CurrentCellDirtyStateChanged(object sender, EventArgs e)
    {
        if (DataGrid.IsCurrentCellDirty)
        {
            DataGrid.CommitEdit(DataGridViewDataErrorContexts.Commit);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-08
      • 2019-07-13
      • 2016-04-21
      相关资源
      最近更新 更多