DataGridViewCheckBoxCell的EditedFormattedValue、FormattedValue属性:
点击方框即可触发DataGridView的CurrentCellDirtyStateChanged事件, EditedFormattedValue=true,在触发其他事件之前,DataGridViewCheckBoxCell的编辑状态未结束,FormattedValue=false;
一旦触发其他事件(点击按钮或者点击下一个方框等(如果点击下一个方框,则触发两次CurrentCellDirtyStateChanged事件)),则会再次触发DataGridView的CurrentCellDirtyStateChanged事件,此时EditedFormattedValue=true,FormattedValue=true;
即触发两次 CurrentCellDirtyStateChanged事件(每完成EditedFormattedValue=true,FormattedValue=true需要触发两CurrentCellDirtyStateChanged事件);
private void dgvHuoWei_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
DataGridViewCheckBoxCell chkBoxCell = new DataGridViewCheckBoxCell();
for (int i = 0; i < dgvHuoWei.Rows.Count; i++)
{
chkBoxCell = (DataGridViewCheckBoxCell)dgvHuoWei.Rows[i].Cells[0];
if (chkBoxCell != null && ((bool)chkBoxCell.EditingCellFormattedValue == true && (bool)chkBoxCell.FormattedValue == true))
{
}
}
}(可以直接在方法中判断 EditedFormattedValue是否为true对选中行操作!最好不在CurrentCellDirtyStateChanged事件中判断,这样容易出错。)
本文探讨了在DataGridView中,当点击DataGridViewCheckBoxCell时,如何理解其选中状态的变化。点击后,EditedFormattedValue变为true,CurrentCellDirtyStateChanged事件被触发。在其他事件发生前,该编辑状态保持,FormattedValue仍为false。当触发其他事件,如点击按钮或下一个单元格,会再次触发事件,此时EditedFormattedValue和FormattedValue皆为true。建议在非CurrentCellDirtyStateChanged事件中处理选中行的操作,以避免错误。
439





