Logo Subtitle Subtitle

Committing a datagrid row (EndEdit) in a viewmodel

By allowing users to edit data in a Silverlight DataGrid, you have to make sure the datarows are properly committed before submitting to the database.
In an MVVM situation I bind the DataGrid to:

public ObservableCollection<Uur> Uren { get; set; }

I save data with the following SaveCommand:

SaveAllCommand = new DelegateCommand<object>(o =>{    Context.SubmitChanges();});

If a datagrid row is this in Edit-mode, this error could occur:
Entity is currently being edited and has uncommitted changes.
A call to BeginEdit must be followed by a call to EndEdit or CancelEdit before changes can be submitted.

Solution

Before submitting, commit all entities:

SaveAllCommand = new DelegateCommand<object>(o =>{
  Uren.ToList().ForEach(uur => (uur as IEditableObject).EndEdit());
  Context.SubmitChanges();
});