具有CheckBox单元格问题的DataGridView

Max*_*Max 8 c# data-binding winforms

我有一个带有DataGridViewCheckBoxColumn列的DataGridView,它被数据绑定到一个列表.问题是此复选框的数据绑定布尔属性不是在选中/取消选中复选框时更新,而是在CellLeave事件之后,换句话说在单元格失去焦点之后更新.我希望在检查/取消选中后立即更新此属性.有一个事件CurrentCellDirtyStateChanged在检查/取消选中后立即触发,因此我可以使用它来手动更新该属性.有一个更好的方法吗?

SwD*_*n81 13

您可以侦听CurrentCellDirtyStateChanged事件并强制提交更改:

void dataGridView1_CurrentCellDirtyStateChanged(object sender,
    EventArgs e)
{
    if (dataGridView1.IsCurrentCellDirty)
    {
        dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
    }
}
Run Code Online (Sandbox Code Playgroud)