简体   繁体   English

根据对话框结果更改datagridview复选框中的检查值

[英]Change checked value in datagridview checkbox cell based on dialog result

I have a situation here. 我这里有个情况。 I have CheckBox column in DataGridView and it is checked by default (when the form loads). 我在DataGridView中具有CheckBox列,并且默认情况下(在加载表单时)将其选中。 Now I have MessageBox for the confirmation that will pop up when someone wants to uncheck CheckBox. 现在,我有MessageBox作为确认消息,当有人要取消选中CheckBox时将弹出该消息。 So when the DialogResult returns cancel, it should come back to normal state (previous value) and once DialogResult returns OK, it should uncheck. 因此,当DialogResult返回cancel时,它应返回到正常状态(以前的值),而DialogResult返回OK后,应取消选中该状态。 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. 我尝试了几个事件(CellValueChanged,CellContentClick,CellBeginEdit,CellClicked,CurrentCellDirtyStateChanged等),但没有任何效果。

CellBeginEdit works for me: CellBeginEdit为我工作:

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM