views:

776

answers:

1

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.

+2  A: 

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.

Henk Holterman
Right, the problem is that I only call Adapter.Update() when the data gets persisted to the database.
Dov
What I guess I'll have to do is to keep my own temporary copy of each row before the user begins an edit operation, and write the values back to the row if they "cancel".
Dov