DataGridView.IsCurrentRowDirty
仍然true
在我將更改提交到數據庫後。我想將它設置爲false
,因此它在失去焦點時不會觸發RowValidating
。我有一個DataGridView
綁定到BindingList<T>
。我處理CellEndEdit
事件並將更改保存到數據庫。在保存這些更改後,我想將DataGridView.IsCurrentRowDirty
設置爲true
,因爲該行中的所有單元都是最新的;但是,它設置爲false
。提交更改後,DataGridView行仍舊髒
這對我造成了問題,因爲當該行失去焦點時,它將觸發RowValidating
,我將處理並驗證所有三個單元格。因此,即使所有單元格都是有效的,沒有髒的,仍然會驗證它們全部。這太浪費了。
這裏是什麼,我有一個例子:
void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
// Ignore cell if it's not dirty
if (dataGridView.isCurrentCellDirty)
return;
// Validate current cell.
}
void dataGridView_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
{
// Ignore Row if it's not dirty
if (!dataGridView.IsCurrentRowDirty)
return;
// Validate all cells in the current row.
}
void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
// Validate all cells in the current row and return if any are invalid.
// If they are valid, save changes to the database
// This is when I would expect dataGridView.IsCurrentRowDirty to be false.
// When this row loses focus it will trigger RowValidating and validate all
// cells in this row, which we already did above.
}
我讀過的職位,說我可以調用窗體的Validate()
方法,但是這將導致RowValidating
火,這是我想避免。
任何想法如何設置DataGridView.IsCurrentRowDirty
到true
?或者也許阻止RowValidating
不必要地驗證所有單元格?
是的,這沒有奏效。當行失去焦點時,「RowValidating」仍然會觸發。工具提示說'EndEdit()'在當前單元格上工作。如果有類似的東西,比如EndRowEdit,我可能會很幸運。還是)感謝你的建議! – Ecyrb
如果有人遇到過像我這樣的人。我通過調用'datagrid.currentrow.datagrid.endedit()'然後'datagrid.endedit'和最後'bindingSource.endedit()'來解決這個問題。 – ppumkin