Datagrid commit changes User validation

Mar 19, 2009 at 4:19 PM
Edited Mar 20, 2009 at 8:31 AM
Hello all.

I just can't figure out how to get the following to work
I have a WCF application to get and save my information this all works wel. Now I want to show all my customers in a datagrid. Also no problem there.

Here comes the part that I do not seem to get working properly. If I edit a row and leave to row I want the user to confirm that the row needs to be saved or cancelled.
On a save I save the data in the collection and to the database On cancel I just revert the changes for this I implemented IEditableObject on my VM.

This is the part that works on partially. When i click outside of the grid I only get one EditRowEnding event. When I use the "EnterKey" I get it twice and when I change a row I get it about 10 times.

I have read alot of topics and articles on this. and The one most people refer to tells me that my VM should implement a Event for the endedit. But I do not want to assing a event handler to every object I might have I just want to handle it form the actions in the Datagrid

Below are the code parts:
 public class CustomerVM : Customer, IEditableObject { public CustomerVM() : base(){}

public CustomerVM(Customer aCustomer) {  

this.Adress = aCustomer.Adress;

this.City = aCustomer.City;

this.ZIPCode = aCustomer.ZIPCode;


sCustomer _copy;

public void BeginEdit() {

if (!IsInEditMode)


/Save the object state

_copy = new Customer {

                    Adress =


ZIPCode = this.ZIPCode


IsInEditMode =




public void CancelEdit()  {

// restore original object state

if (_copy != null) {

this.Adress = _copy.Adress;

this.ZIPCode = _copy.ZIPCode;


// clear cached data

_copy = null;

// ensure edit mode flag is unset

IsInEditMode =false;



public void EndEdit() {

// clear cached data



copy = null;

// ensure edit mode flag is unset

IsInEditMode = false;



private bool _isInEditMode = false;

public bool IsInEditMode {

get { return _isInEditMode; }

private set{ if (_isInEditMode != value) {


_isInEditMode =








this.dgCustomers.ItemsSource = ServiceController.GetInstance().Customers;

this.dgCustomers.RowEditEnding += new EventHandler<DataGridRowEditEndingEventArgs>(dgCustomers_RowEditEnding);



void dgCustomers_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)



CustomerVM cust = e.Row.Item as CustomerVM;

if (cust != null && cust.IsInEditMode && e.EditAction == DataGridEditAction.Commit)



if (MessageBox.Show(string.Format("Wil je de gegevens van {0} opslaan?", cust.Name), "Gegevens opslaan", MessageBoxButton.OKCancel) == MessageBoxResult.OK)