Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
425 views
in Technique[技术] by (71.8m points)

c# - Programmatically check a DataGridView CheckBox that was just unchecked

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...