DataGridView row is still dirty after changes

DataGridView.IsCurrentRowDirty remains true after making changes to the database. I want to set it to false so that it does not start RowValidating when it has lost focus.

I have a DataGridView associated with a BindingList<T> . I handle the CellEndEdit event and save the changes to the database. After saving these changes, I would like DataGridView.IsCurrentRowDirty be set to true , since all cells in the row have been updated; however, it is set to false .

This causes problems for me, because when a row loses focus, it causes RowValidating , with which I process and check all three cells. Therefore, despite the fact that all the cells are valid and not one of them is contaminated, he still checks them all. This is waste.

Here is an example of what I have:

 void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { // Ignore cell if it not dirty if (dataGridView.isCurrentCellDirty) return; // Validate current cell. } void dataGridView_RowValidating(object sender, DataGridViewCellCancelEventArgs e) { // Ignore Row if it 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. } 

I read the messages saying that I can call the Validate() method, but this will trigger RowValidating , which I am trying to avoid.

Any idea how I can set DataGridView.IsCurrentRowDirty to true ? Or maybe there is a way to prevent RowValidating from unnecessarily checking all cells?

+4
source share
2 answers

You tried to call DataGridView1.EndEdit () after saving the data in the database.

+3
source

I had the same problem when the Validating arrow fired twice. Once before editing (as expected), but again on the fact that, as I think, the focus of the DataGridView has changed.

There was no time for investigation. Quick fix this.ActiveControl = null; I'm not sure if this has any unintended consequences, but it fixes the verification problem without programmatically focusing the control.

 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; } 
0
source

All Articles