0
votes

I have a situation here. I have CheckBox column in DataGridView and it is checked by default (when the form loads). Now I have MessageBox for the confirmation that will pop up when someone wants to uncheck CheckBox. So when the DialogResult returns cancel, it should come back to normal state (previous value) and once DialogResult returns OK, it should uncheck. I have tried many cell events which doesn't suit my situation all will fire after cell change its value. I want to trigger snippets before it changes the value.

I tried several events (CellValueChanged,CellContentClick, CellBeginEdit, CellClicked,CurrentCellDirtyStateChanged etc.) but nothing worked.

1
which cell events have you tried already? can you also include code of your best attempt?ASh
do you want your dialog to appear only for the very first time a check box is clicked ? or on every click ?Kapoor
@Kapoor it should be for every click.Sujith H S

1 Answers

0
votes

CellBeginEdit works for me:

private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
    bool oldVal = (bool)dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;

    DialogResult r = MessageBox.Show("Really?", "", MessageBoxButtons.OKCancel);
    if (r == DialogResult.Cancel)
    {
        e.Cancel = true;
    }
    else
    {
        dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = !oldVal;
    }
}