DataGridView中DataGridViewCheckBoxCell点击选中状态的理解

本文探讨了在DataGridView中,当点击DataGridViewCheckBoxCell时,如何理解其选中状态的变化。点击后,EditedFormattedValue变为true,CurrentCellDirtyStateChanged事件被触发。在其他事件发生前,该编辑状态保持,FormattedValue仍为false。当触发其他事件,如点击按钮或下一个单元格,会再次触发事件,此时EditedFormattedValue和FormattedValue皆为true。建议在非CurrentCellDirtyStateChanged事件中处理选中行的操作,以避免错误。

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事件中判断,这样容易出错。

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值