C# – DataGridView Save Changes On Row Change

cdatagridviewwinforms

I am attempting to save a record after leaving the row in a DataGridView.

I have seen solutions use the RowValidated event, however, when the rows are sorted then the record gets resorted before the RowValidation event is fired. I also attempted to get the row using the BindingSource Current property, but the current property is already changed. Same applies to RowLeaving I believe.

I have tried using the RowChanged event on the datatable directly and that does work OK.

I ended up using the RowValidation event and getting the changes from the datatable (GetChange()), but this doesn’t seem idea.

In addition for deletes I used the UserDeletingRow event, but again this doesn’t seem idea.

What is the best way for saving records after leaving the row?

Best Answer

Have you tried the DataGridView.RowValidating Event and .IsCurrentRowDirty?

    private void dataGridView1_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
    {
        if (dataGridView1.IsCurrentRowDirty)
        {
           //Do stuff
        }
    }
Related Topic