Programmatically check a DataGridView CheckBox that was just unchecked

北慕城南 提交于 2020-08-08 05:18:37

问题


I am aware that similar questions have been asked before, but none of the solutions are helping me.

I have a DataGridViewCheckBoxColumn in an unbound DataGridView.
In the CellContentClick event, when a CheckBox is unchecked, I am prompting the user whether they want to continue with this operation according to the business rules behind the DataGridView and, if they choose not to continue, I want to re-check the CheckBox.

It is this re-checking of the CheckBox that is not working.

Here is my code:

private void dgvPeriods_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == dgvPeriods.Columns["colSelected"].Index)
    {
        dgvPeriods.CommitEdit(DataGridViewDataErrorContexts.Commit);
        DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)dgvPeriods[e.ColumnIndex, e.RowIndex];

        if (chk.Value = chk.FalseValue)
        {
            If (MessageBox.Show("Continue with this Operation?", "Continue",  MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
            {
                chk.Value = chk.TrueValue;
                return;
            }
        }
    }
}

The value of the cell is being set, but visually the CheckBox is not checked.

If have tried different types for the TrueValue and FalseValue (booleans vs strings), I have tried calling Refresh(), I have tried calling CommitEdit(), I have tried using CheckState.Checked.

What can I do to visually re-check the CheckBox ?


回答1:


You can commit the edit immediately after a CellContentClick event is raised, using the (proper) EndEdit() method, so the CellValueChanged1 event is also raised immediately instead of after the current Cell loses focus.

Evaluate here the new Value: since the Value has changed, it's intended that the current value is the opposite of the previous, give that this is a bool Column.

At this point, if the User confirms the choice made, you reset the value and call RefreshEdit() to redraw the CheckBox in its current state.

Note: the behavior of your DataGridView may depend on the context of your operations.

private void dgvPeriods_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex != dgvPeriods.Columns["colSelected"].Index) return;

    bool newValue = (bool)dgvPeriods[e.ColumnIndex, e.RowIndex].Value;

    if (!newValue) {
        if (MessageBox.Show("Continue with this Operation?", "Continue", 
            MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) {
            dgvPeriods[e.ColumnIndex, e.RowIndex].Value = true;
            dgvPeriods.RefreshEdit();
        }
    }
}

private void dgvPeriods_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    // You need to evaluate whether EndEdit() applies to just this Column 
    if (e.ColumnIndex != dgvPeriods.Columns["colSelected"].Index) return;
    dgvPeriods.EndEdit();
}

1 - Note that this event is raised really immediately, before the code in the previous event handler completes



来源:https://stackoverflow.com/questions/62029355/programmatically-check-a-datagridview-checkbox-that-was-just-unchecked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!