DataGridView的Validating事件注册后删除操作的处理
我們在處理DataGridView必填項判斷時,一般使用DataGridView的RowValidating事件判斷,具體代碼如下:
protected override void OnRowValidating(DataGridViewCellCancelEventArgs e)
{
base.OnRowValidating(e);
if (NecessaryColIndexArray != null)
{
for (int i = 0; i < Rows.Count; i++)
{
if (Rows[i].IsNewRow)
continue;
//this.delete
for (int m = 0; m < NecessaryColIndexArray.Length; m++)
{
//判D斷?行D數(shù)ºy是º?否¤?超?出?集¡¥合?
if (NecessaryColIndexArray[m] >= 0 && NecessaryColIndexArray[m] < Columns.Count - 1)
{
if (Rows[i].Cells[NecessaryColIndexArray[m]].Value==null
||
string.IsNullOrEmpty(Rows[i].Cells[NecessaryColIndexArray[m]].Value.ToString()))
{
MessageTools.ShowMsg("列¢D"+Columns[NecessaryColIndexArray[m]].HeaderText+"是º?否¤?刪¦?除y第̨²
¬?項?,ê?必À?須?填¬?寫¡ä內(nèi)¨²容¨Y。¡ê");
e.Cancel = true;
}
}
}
} }
}
其中:NecessaryColIndexArray為Int[]型屬性,卸載自定義DataGridView控件中。
表示必填項列的索引。
這樣我們就可以實現(xiàn)必填項判斷了。
但是這里還有一個問題:
當我們處理刪除事件時,尤其是我們刪除必填項沒有填寫完全的行的問題時。
這個Validating會一直觸發(fā)。而無法完成目標。這是因為我們點擊刪除按鈕時,
DataGridview失去焦點而刪除按鈕獲得焦點,這樣會引發(fā)Validating事件。
所以解決方法如下:
在datagridView中添加一個判斷條件bool isDeleting = false;
并且將本datagridView的刪除按鈕作為一個屬性添加到我們的自定義datagridView控件中。
這樣我們在datagridview控件的LostFocus事件中可以判斷是否刪除按鈕獲得了焦點,
如果是刪除按鈕獲得焦點,那么就我進行validating判斷語句執(zhí)行。
bool isDeleting = false;
protected override void OnLostFocus(EventArgs e)
{
base.OnLostFocus(e);
//判D斷?是º?否¤?刪¦?除y按ã¡ä鈕£¤獲?取¨?焦1點Ì?,ê?并¡é且¨°當Ì¡À時º¡À刪¦?除y的Ì?時º¡À Validating有®D錯䨪誤¨®的Ì?行D,ê?
//即¡äCurrentCell為a當Ì¡À前¡ã選?中D,ê?停ª¡ê止1Validating
if (btnDel != null)
isDeleting = btnDel.Focused;
} protected override void OnRowValidating(DataGridViewCellCancelEventArgs e)
{
base.OnRowValidating(e);
if (NecessaryColIndexArray != null&&!isDeleting)
{
for (int i = 0; i < Rows.Count; i++)
{
if (Rows[i].IsNewRow)
continue;
//this.delete
for (int m = 0; m < NecessaryColIndexArray.Length; m++)
{
//判D斷?行D數(shù)ºy是º?否¤?超?出?集¡¥合?
if (NecessaryColIndexArray[m] >= 0 && NecessaryColIndexArray[m] < Columns.Count - 1)
{
if (Rows[i].Cells[NecessaryColIndexArray[m]].Value==null
||
string.IsNullOrEmpty(Rows[i].Cells[NecessaryColIndexArray[m]].Value.ToString()))
{
MessageTools.ShowMsg("列¢D:'"+Columns[NecessaryColIndexArray[m]].HeaderText+"'是º?必À?填¬?項?,ê?必À?須?填¬?寫¡ä內(nèi)¨²容¨Y。¡ê");
e.Cancel = true;
}
}
}
} }
}
這么做基本滿足要求。但是有一個BUG就是我選擇刪除按鈕,但是不單擊它,就會忽略validating事件。結果方法如下:
轉(zhuǎn)移到 刪除按鈕時 在按鈕tag上加判斷。1為完成Click
protected override void OnLostFocus(EventArgs e)
{
base.OnLostFocus(e);
//判D斷?是º?否¤?刪¦?除y按ã¡ä鈕£¤獲?取¨?焦1點Ì?,ê?并¡é且¨°當Ì¡À時º¡À刪¦?除y的Ì?時º¡À Validating有®D錯䨪誤¨®的Ì?行D,ê?
//即¡äCurrentCell為a當Ì¡À前¡ã選?中D,ê?停ª¡ê止1Validating
if (btnDel != null)
{
isDeleting = btnDel.Focused;
btnDel.Tag = 0;
}
}
點擊事件
void btnDel_Click(object sender, EventArgs e)
{
btnDel.Tag = 1;
btnDelClick();
}
刪除按鈕的lostfocus事件中判斷
if (btnDel.Tag.ToString() != "1"&&isDeleting)
{
isDeleting = false;
OnRowValidating(new DataGridViewCellCancelEventArgs(0, CurrentCell.RowIndex));
}
這樣就可以比較完美的解決此問題。但是這么解決不是從本質(zhì)上解決這個問題雖然目前還沒有發(fā)現(xiàn)什么BUG,
但是不知道是否有BUG,而且適用性也不是很廣。需要設置的也很多,
刪除按鈕必須關聯(lián)到datagridView,而且刪除事件也是卸載datagridView控件中的。
總結
以上是生活随笔為你收集整理的DataGridView的Validating事件注册后删除操作的处理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: POJ 2299
- 下一篇: python中的列表和元组