How to detect DataGridView CheckBox event change?

后端 未结 16 2459
离开以前
离开以前 2020-11-27 12:43

I have a winforms app and want to trigger some code when a checkbox embedded in a DataGridView control is checked / unchecked. Every event I have tried either

16条回答
  •  伪装坚强ぢ
    2020-11-27 13:44

    What worked for me was CurrentCellDirtyStateChanged in combination with datagridView1.EndEdit()

    private void dataGridView1_CurrentCellDirtyStateChanged( object sender, EventArgs e ) {
        if ( dataGridView1.CurrentCell is DataGridViewCheckBoxCell ) {
            DataGridViewCheckBoxCell cb = (DataGridViewCheckBoxCell)dataGridView1.CurrentCell;
            if ( (byte)cb.Value == 1 ) {
                dataGridView1.CurrentRow.Cells["time_loadedCol"].Value = DateTime.Now.ToString();
            }
        }
        dataGridView1.EndEdit();
    }
    

提交回复
热议问题