tags:

views:

511

answers:

2

In my view, I've implemented a the WPF DataGrid from CodePlex:

<toolkit:DataGrid x:Name="CodePlexDataGrid" 
    Style="{StaticResource ToolkitDataGrid}" 
    ItemsSource="{Binding Customers}"/>

It is bound to an ObservableCollection in my ViewModel:

private ObservableCollection<Customer> _customers;
public ObservableCollection<Customer> Customers
{
    get
    {
        return _customers;
    }

    set
    {
        _customers = value;
        OnPropertyChanged("Customers");
    }
}

When I change data in the grid, it changes, but I can find no event that I can handle to catch those changes, e.g. DataGridCellChanged so that I can save the data that was entered back into the database.

What is the process by which we can capture the changes to the cells and save them back to the database?

A: 

I have been using the events CellEditEnding and RowEditEnding, won't they fit your need?

Oskar
A: 

Try approaching it a different way. Instead of binding to events on the DataGrid, implement INotifyPropertyChanged on Customer and handle the property changed events of the Customer objects. In WPF (as opposed to Silverlight) I think you can use BindingList instead of ObservableCollection in order to watch property changes for any item in the collection.

For Silverlight I created a subclass of ObservableCollection that wired up PropertyChanged event handlers for any item added to the collection then bubbled them up to an ItemPropertyChanged event exposed by the collection.

That way I could do:

myCollection.ItemPropertyChanged += (sender,e) => {
   // sender is the item whose property changed
   // e is PropertyChangedEventArgs which has the name of the property that changed
}
Josh Einstein