views:

827

answers:

1

DataGridView.IsCurrentRowDirty remains true after I commit changes to the database. I want to set it to false so it doesn't trigger RowValidating when it loses focus.

I have a DataGridView bound to a BindingList<T>. I handle the CellEndEdit event and save changes to the database. After saving those changes I would like DataGridView.IsCurrentRowDirty to be set to true, since all cells in the row are up-to-date; however, it's set to false.

This causes problems for me because when the row does lose focus it will trigger RowValidating, which I handle and validate all three cells in. So even though all the cells are valid and none are dirty it will still validate them all. That's a waste.

Here's an example of what I have:

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

I've read posts that said I could call the form's Validate() method, but that will cause RowValidating to fire, which is what I'm trying to avoid.

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

+1  A: 

Have you tried calling DataGridView1.EndEdit() after saving the data to the database.

rip
Yeah, that didn't work. `RowValidating` still fires when the row loses focus. The tooltip says `EndEdit()` works on the current cell. If there was something similar, like a EndRowEdit, I might be in luck. Thanks for the suggestion, though!
Ecyrb