Triggering a checkbox value changed event in DataGridView

前端 未结 15 2912
一生所求
一生所求 2020-12-01 08:02

I have a grid view that has a check box column, and I want to trigger a drawing event as soon as the value of the cell is toggled. I tried the ValueChaged and the CellEndEdi

15条回答
  •  天涯浪人
    2020-12-01 08:23

    Every one of the CellClick and CellMouseClick answers is wrong, because you can change the value of the cell with the keyboard and the event will not fire. Additionally, CurrentCellDirtyStateChanged only fires once, which means if you check/uncheck the same box multiple times, you will only get one event. Combining a few of the answers above gives the following simple solution:

    private void dgvList_CurrentCellDirtyStateChanged(object sender, EventArgs e)
    {
        if (dgvList.CurrentCell is DataGridViewCheckBoxCell)
            dgvList.CommitEdit(DataGridViewDataErrorContexts.Commit);
    }
    
    private void dgvList_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        // Now this will fire immediately when a check box cell is changed,
        // regardless of whether the user uses the mouse, keyboard, or touchscreen.
        //
        // Value property is up to date, you DO NOT need EditedFormattedValue here.
    }
    

提交回复
热议问题