How can I determine whether a checkbox is checked?

Spideregg 140 Reputation points
2023-09-27T03:47:07.1666667+00:00
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.

Developer technologies | C#
Developer technologies | 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.
0 comments No comments
{count} votes

Answer accepted by question author
  1. KOZ6.0 6,735 Reputation points
    2023-09-27T05:11:24.7033333+00:00

    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 125.7K Reputation points
    2023-09-27T04:48:03.2766667+00:00

    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' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.