Share via

How can I determine whether a checkbox is checked?

Spideregg 140 Reputation points
Sep 27, 2023, 3:47 AM
private void dgvSearch_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == colChecked1.Index)
            {
                DataGridViewCheckBoxCell checkBoxCell = dgvSearch.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewCheckBoxCell;

                if ((bool)checkBoxCell.Value)
                {
                    Messager.Error("This CheckBox already checked");
                    return;
                }
            }
        }

174.drawio

Before the user clicks the checkbox,and if the checkbox is unchecked, the error will not be displayed.
How can I implement this ? Thank you.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,065 questions
0 comments No comments
{count} votes

Accepted answer
  1. KOZ6.0 6,590 Reputation points
    Sep 27, 2023, 5:11 AM

    When try to change the value of a DataGridVIew, the CurrentCellDirtyStateChanged event occurs and IsCurrentCellDirty property becomes true. At this time, when retrieve the cell value, it contains the value before the change.(However, appearance of the cell has changed.) Executing CancelEdit method cancels the changes, and executing CommitEdit method accepts the changes.

    private void dgvSearch1_CurrentCellDirtyStateChanged(
                                    object sender, EventArgs e) {
        var dgv = (DataGridView)sender;
        var current = dgv.CurrentCell;
        if (dgv.IsCurrentCellDirty && current != null) {
            if (current.ColumnIndex == colChecked1.Index) {
                bool prevValue = (bool)current.Value;
                if (prevValue) {
                    Messager.Error("This CheckBox already checked");
                    dgv.CancelEdit();
                } else {
                    dgv.CommitEdit(DataGridViewDataErrorContexts.Commit);
                }
            }
        }
    }
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Viorel 118.4K Reputation points
    Sep 27, 2023, 4:48 AM

    The CellValueChanged event seems to be suitable, because it also handles the keyboard. However, according to documentation, it is necessary to use the CurrentCellDirtyStateChanged event too:

    Therefore, try to remove the CellContentClick handler and use these events:

    private void dgvSearch_CurrentCellDirtyStateChanged( object sender, EventArgs e )
    {
        if( dgvSearch.IsCurrentCellDirty ) dgvSearch.CommitEdit( DataGridViewDataErrorContexts.Commit );
    }
    
    private void dgvSearch_CellValueChanged( object sender, DataGridViewCellEventArgs e )
    {
        if( e.RowIndex >= 0 && e.ColumnIndex == colChecked1.Index )
        {
            DataGridViewCheckBoxCell checkBoxCell = dgvSearch.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewCheckBoxCell;
    
            if( (bool)checkBoxCell.Value )
            {
                Messager.Error( "This CheckBox already checked" );
                return;
            }
        }
    }
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.