Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WinForms中的DataGridViewCheckBox单元格检查事件_C#_.net_Winforms_Datagridview - Fatal编程技术网

C# WinForms中的DataGridViewCheckBox单元格检查事件

C# WinForms中的DataGridViewCheckBox单元格检查事件,c#,.net,winforms,datagridview,C#,.net,Winforms,Datagridview,我有未绑定的DataGridViewCheckBox列。当用户更改检查状态时,我需要执行方法来检查每一行的状态,查找复选框列,并基于该检查状态对其他列进行一些计算 到目前为止,我尝试过的每个事件:CellContentClick、CellMouseUp、cellvalized、CellValueChanged在我离开单元格或抓取DataGridViewCheckBox`检查更改操作之前的状态 什么事件允许在更改后但在我离开单元格或行之前获取值?首先需要设置DataGridViewCheckBox

我有未绑定的
DataGridViewCheckBox
列。当用户更改检查状态时,我需要执行方法来检查每一行的状态,查找复选框列,并基于该检查状态对其他列进行一些计算

到目前为止,我尝试过的每个事件:
CellContentClick
、CellMouseUp
cellvalized
CellValueChanged
在我离开单元格或抓取
DataGridViewCheckBox`检查更改操作之前的状态


什么事件允许在更改后但在我离开单元格或行之前获取
值?

首先需要设置DataGridViewCheckBoxColumn TrueValue和FalseValue属性。然后,您应该处理DataGridView CellContentClick和CellValueChanged,以确定是否选中该单元格

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        var checkBoxColumn = (DataGridViewCheckBoxColumn)this.dataGridView1.Columns[0];
        checkBoxColumn.TrueValue = true;
        checkBoxColumn.FalseValue = false;

        this.dataGridView1.CellContentClick += new DataGridViewCellEventHandler(dataGridView1_CellContentClick);
        this.dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
    }

    private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        DataGridViewCheckBoxCell cell = (sender as DataGridView).Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewCheckBoxCell;

        if (cell != null)
        {
            if (cell.Value == cell.TrueValue)
            {
                MessageBox.Show("Cell checked.");
            }
            else
            {
                MessageBox.Show("Cell unchecked.");
            }
        }
    }

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        var dataGridView = sender as DataGridView;

        if (dataGridView.Columns[e.ColumnIndex] is DataGridViewCheckBoxColumn)
        {
            // Raise CellValueChanged
            dataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
        }
    }
}