Triggering a checkbox value changed event in DataGridView

前端 未结 15 2905
一生所求
一生所求 2020-12-01 08:02

I have a grid view that has a check box column, and I want to trigger a drawing event as soon as the value of the cell is toggled. I tried the ValueChaged and the CellEndEdi

15条回答
  •  悲&欢浪女
    2020-12-01 08:26

    Using the .EditedFormattedValue property solves the problem

    To be notified each time a checkbox in a cell toggles a value when clicked, you can use the CellContentClick event and access the preliminary cell value .EditedFormattedValue.

    As the event is fired the .EditedFormattedValue is not yet applied visually to the checkbox and not yet committed to the .Value property.

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
       var checkbox = dataGridView1.CurrentCell as DataGridViewCheckBoxCell;
    
       bool isChecked = (bool)checkbox.EditedFormattedValue;
    
    }
    

    The event fires on each Click and the .EditedFormattedValue toggles

提交回复
热议问题