3
DatagridView
讓我再次發瘋。所以我有一個DatagridView
的表格,直到現在。但現在數據庫中存在無效值,導致Validating
事件阻止程序流。無法編輯DataGridView單元格,驗證事件集e.Cancel = true
這是它:
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 !!!!
註釋行導致事件被取消,其阻斷GUI。用戶不能編輯這個無效值。
在數據綁定期間,我已取消訂閱此事件以禁用它。但是現在,如果用戶點擊單元格來編輯無效值,它仍然會被觸發。調用堆棧窗口顯示它是從CellMouseDown
-event內部觸發的。我怎樣才能防止這個?我希望只有在用戶編輯了一個單元並離開它時才能對其進行驗證。
謝謝,這是一個好主意,適用於我的情況。但是,我仍然對一種始終有效的不同方法感興趣。 – 2015-02-24 13:59:00
耶同意了。雖然我猜如果基礎數據未通過驗證,那麼必須有一種方法告訴驗證處理程序忽略該單元 – 2015-02-24 14:39:00