C# – Undoing All Changes Since BindingSource’s last EndEdit call

ado.netbindingsourcecdata-bindingnet

Here's the scenario (which uses a BindingSource bound to a DataTable within a DataSet):

  1. A user creates a new address book
    contact, fills in the First and Last
    name in data-bound controls.
  2. He presses Apply, whose event
    handler calls
    BindingSource.EndEdit().
  3. He then realizes there was a
    mistake, and adds an email address.
  4. But when he presses Apply,
    validation fails (invalid email
    format), so EndEdit() isn't called.
  5. He decides not to make the edit, and
    presses the Cancel button, whose
    event handler calls
    BindingSource.CancelEdit().
  6. But, rather than reverting to the
    new contact with just a First and
    Last name and no email, the
    BindingSource has instead gotten rid
    of the entire record.

Is there any way to only undo actions since the last time EndEdit() was called? I was under the impression that's how CancelEdit() was supposed to work.

Best Answer

As an explanation, the DataTable only holds 2 states for a record, the Original and Current. Your request would require multiple states.

To achieve what you want you should flush the changes to the database (eg Adapter.Update(table)) in response to a successful Apply. That promotes your Current to Original and the next Cancel can fall back to that.

This may or may not match your requirements though.