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

来源:互联网 发布:阿里云服务器登陆密码 编辑:程序博客网 时间:2024/05/16 05:23

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

0 0
原创粉丝点击