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