繁体   English   中英

单击和取消选中复选框单元格时的DataGridView运行方法

[英]DataGridView run method when clicking and unclicking a checkbox cell

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

检查DataGridView的事件,似乎没有什么可以直接监视checkboxcell中的更改。

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

经过更多的搜索后,我发现获取更改后的值的最佳方法是检查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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM