![](/img/trans.png)
[英]What are other alternatives to using e.Cancel = true in Validating event?
[英]Cannot edit DataGridView-cell, Validating-event sets e.Cancel = true
DatagridView
讓我再次瘋狂。 所以我有一個帶有DatagridView
的表單,直到現在才有效。 但是現在數據庫中存在無效值,導致Validating
事件阻止程序流。
就是這個:
private void GrdChargeAssignment_Validating(Object sender, DataGridViewCellValidatingEventArgs e)
{
e.Cancel = false;
var grid = (DataGridView)sender;
ErpService.ArrivalChargeAssignment ass = grid.Rows[e.RowIndex].DataBoundItem as ErpService.ArrivalChargeAssignment;
string countValue = grid.Rows[e.RowIndex].Cells[AssignedCol.Name].EditedFormattedValue.ToString();
if (string.IsNullOrWhiteSpace(countValue))
{
grid.Rows[e.RowIndex].Cells[AssignedCol.Name].Value = "0";
countValue = "0";
}
int count;
if (!int.TryParse(countValue, out count))
{
grid.Rows[e.RowIndex].ErrorText = "Insert a valid integer for count!";
e.Cancel = true;
}
else if (count > ass.Count_Actual)
{
grid.Rows[e.RowIndex].ErrorText = string.Format("Please insert a count between 0 and arrival-count({0})!", ass.Count_Actual);
e.Cancel = true; // !!!! HERE !!!!
}
if (e.Cancel == false)
grid.Rows[e.RowIndex].ErrorText = "";
}
我評論過的那條線!!!! HERE !!!!
!!!! HERE !!!!
導致事件被取消阻止gui。 用戶無法編輯此無效值。
在數據綁定期間,我已經取消訂閱此事件以禁用它。 但現在,如果用戶單擊進入單元格以編輯無效值,它仍會被觸發。 調用堆棧窗口顯示它是從CellMouseDown
-event內部觸發的。 我該如何防止這種情況? 我希望只有在用戶編輯了一個單元格並離開它時才能驗證它。
如果您只想在用戶更改值時進行驗證,是否可以在應用驗證之前檢查修改? 就像是:
else if (count > ass.Count_Actual)
{
if( count == ass.Count )
{
// The data has not changed, do not validate
}
else
{
grid.Rows[e.RowIndex].ErrorText = string.Format("Please insert a count between 0 and arrival-count({0})!", ass.Count_Actual);
e.Cancel = true; // !!!! HERE !!!!
}
}
如果用戶將值編輯為另一個無效值,則驗證將啟動,否則將忽略錯誤數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.