none
DataGridView CheckBox Column uncheck does not make its value false

    Question

  • Hi guys!

    I have datagridview with a select column. when i check a checkbox in DataGrid an again make it unchecked value would not change to false!

    C#

    Win app

    .Net 2.0

    VS2008 SP1

    Thank you!

    Sunday, October 17, 2010 7:00 PM

Answers

  • Hi,

     

    Check box cell values are intended either for storage, like any other data, or for performing bulk operations.

    If you want to respond immediately when users click a check box cell, you can handle the DataGridView.CellClick event, but this event occurs before the cell value is updated.

     If you need the new value at the time of the click, one option is to calculate what the expected value will be based on the current value. Another approach is to commit the change immediately, and handle the DataGridView.CellValueChanged event to respond to it. To commit the change when the cell is clicked, you must handle the DataGridView.CurrentCellDirtyStateChanged event. In the handler, if the current cell is a check box cell, call the DataGridView.CommitEdit method and pass in the Commit value.

    In DataGridView.CurrentCellDirtyStateChanged Event msdn document, it gives an example:

     

    // This event handler manually raises the CellValueChanged event

    // by calling the CommitEdit method.

    void dataGridView1_CurrentCellDirtyStateChanged(object sender,

        EventArgs e)

    {

        if (dataGridView1.IsCurrentCellDirty)

        {

            dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);

        }

    }

     

    // If a check box cell is clicked, this event handler disables 

    // or enables the button in the same row as the clicked cell.

    public void dataGridView1_CellValueChanged(object sender,

        DataGridViewCellEventArgs e)

    {

        if (dataGridView1.Columns[e.ColumnIndex].Name == "CheckBoxes")

        {

            DataGridViewDisableButtonCell buttonCell =

                (DataGridViewDisableButtonCell)dataGridView1.

                Rows[e.RowIndex].Cells["Buttons"];

     

            DataGridViewCheckBoxCell checkCell =

                (DataGridViewCheckBoxCell)dataGridView1.

                Rows[e.RowIndex].Cells["CheckBoxes"];

            buttonCell.Enabled = !(Boolean)checkCell.Value;

     

            dataGridView1.Invalidate();

        }

    }

     

     

    More information about datagridviewCheckboxCell: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcheckboxcell.aspx

     

    Best regards,

    Ling Wang


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Wednesday, October 20, 2010 8:52 AM