1. 程式人生 > >DataGridView中DataGridViewCheckBoxCell點選選中狀態的理解

DataGridView中DataGridViewCheckBoxCell點選選中狀態的理解

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事件中判斷,這樣容易出錯。