提交更改后,DataGridView行仍然是脏的

在我提交对数据库的更改后, DataGridView.IsCurrentRowDirty仍然为true 。 我想将其设置为false因此当它失去焦点时它不会触发RowValidating

我有一个绑定到BindingListDataGridView 。 我处理CellEndEdit事件并保存对数据库的更改。 保存这些更改后,我希望将DataGridView.IsCurrentRowDirty设置为true ,因为该行中的所有单元格都是最新的; 但是,它设置为false

这会给我带来问题,因为当行失去焦点时会触发RowValidating ,我会处理并validation所有三个单元格。所以即使所有单元格都有效且没有一个是脏的,它仍然会validation它们。 那是浪费。

这是我的一个例子:

 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. } 

我已经阅读过post,说我可以调用表单的Validate()方法,但这会导致RowValidating触发,这是我试图避免的。

知道如何将DataGridView.IsCurrentRowDirty设置为true ? 或者也许是一种防止RowValidating不必要地validation所有单元格的方法?

您是否在将数据保存到数据库后尝试调用DataGridView1.EndEdit()。

我在Validating两次Validating遇到了同样的问题。 在编辑之前(正如预期的那样)编辑,然后在我认为DataGridView焦点更改时再次。

没有时间去调查。 快速修复是this.ActiveControl = null; 我不确定这是否有任何意想不到的后果,但它通过以编程方式取消控制来修复validation问题。

 private void cntrl_MethodParameters_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { //Init var dgv = (DataGridView)sender; int row = e.RowIndex; int col = e.ColumnIndex; //Validate Edit if ((row >= 0) && (col == cntrl_MethodParameters.Columns.IndexOf(cntrl_MethodParameters.Columns[MethodBuilderView.m_paramValueCol]))) { string xPropertyName = (string)cntrl_MethodParameters[MethodBuilderView.m_paramNameCol, row].EditedFormattedValue; string xPropertyValue = (string)cntrl_MethodParameters[MethodBuilderView.m_paramValueCol, row].EditedFormattedValue; bool Validated = FactoryProperties.Items[xPropertyName].SetState(xPropertyValue); //Cancel Invalid Input if (!Validated) { dgv.CancelEdit(); } } this.ActiveControl = null; }