DataGridView 与 CheckBox 单元格问题 [英] DataGridView with CheckBox cell problem
问题描述
我有一个带有 DataGridViewCheckBoxColumn 列的 DataGridView,该列数据绑定到一个列表.问题是此复选框的数据绑定布尔属性不是在选中/取消选中复选框时更新,而是在 CellLeave 事件之后更新,换句话说,在单元格失去焦点之后.我希望在选中/取消选中后立即更新此属性.有一个事件 CurrentCellDirtyStateChanged 在选中/取消选中后立即触发,因此我可以使用它来手动更新属性.有没有更好的方法来做到这一点?
I have a DataGridView with a DataGridViewCheckBoxColumn column, which is databound to a list. The problem is that the databound boolean property for this checkbox is updated not when the check box is checked/unchecked, but after the CellLeave event in other words after the cell looses focus. I want this property to be updated right after the check/uncheck. There's an event CurrentCellDirtyStateChanged which is fired right after check/uncheck happens, so I can use it to update the propery manually. Is there a better way to do this?
推荐答案
您可以收听 CurrentCellDirtyStateChanged 事件并强制提交更改:
You can listen for the CurrentCellDirtyStateChanged event and force Commit the change:
void dataGridView1_CurrentCellDirtyStateChanged(object sender,
EventArgs e)
{
if (dataGridView1.IsCurrentCellDirty)
{
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
这篇关于DataGridView 与 CheckBox 单元格问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!