Row Editing Events

Nov 14, 2008 at 11:03 PM
I'd like to commit changes to my database whenever a row is added, updated or deleted in the DataGrid. What's the best way to do that? I was looking for something equivalent to the XamDataGrid's RecordAdding/ed, RecordUpdating/ed and RecordsDeleting/ed events ... The closest I could find was RowEditEnding. It looks like that might work for adds and updates, but not deletes. Should I be using that? Would anyone be willing to post a code sample?

Thanks!

Nov 15, 2008 at 4:29 AM
Just thought of another related question: When a user tries to delete a row in the DataGrid, which event would allow me to throw up a MessageBox to confirm the action?


Nov 15, 2008 at 4:48 AM
Edited Nov 16, 2008 at 12:39 AM
It looks like the RowEditEnding event may not be exactly what I want. It seems like it fires whenever any cell in a row is leaving edit mode ... so if a user is tabbing through the cells in a row and editing values, the event will fire every time focus moves to another cell. What I'm looking for is a simple way to know when the user is done editing the entire row.
Coordinator
Nov 17, 2008 at 1:52 PM
RowEditEnding is the event you want.  It is fired when committing and cancelling so you'll want to check the parameter e.EditAction for a commit action.  It should not fire multiple times when tabbing unless you have columns that are readonly.  If you are using ADO.net, you also have the RowChanged event that you could use to commit back to the database.

For the delete functionality, you either need to listen for the Delete command through the command manager and do the work there or listen for the Delete keydown and do the work there.
Nov 17, 2008 at 1:58 PM
Hi Peter,

You might be interested in this article which has a complete example of binding a DataGrid to a DataSet:

http://www.codeproject.com/KB/WPF/WPFDataGridExamples.aspx

The RowChanged and RowDeleted events (as Vincent mentioned) from the DataTable are used to sycnhronise the database state.

Colin E.


Nov 17, 2008 at 3:30 PM
Thank you both for your help.

It looks like my mistake with RowEditEnding was not to have the objects in my collection implement IEditableObject. When they don't implement that interface, RowEditEnding seems to fire on every cell. When they do, everything works as expected. Hopefully by posting this to the forum I'll save someone else from banging their head against the wall for a couple hours. :)

It looks like there are a number of options for handling deletes: hooking up to the DataGrid's KeyDown, using the CommandManager or, as Colin suggested on a thread at CodeProject, handling the CollectionChanged event on the underlying ObservableCollection (or ICollectionView, too, I guess).

Thanks again.

Best,
Peter
May 12, 2009 at 4:09 PM

If anybody else was looking for an example on how to confirm the delete operation with a message box by:

"listen for the Delete command through the command manager"

 

Add this to your window constructor:

CommandManager.AddPreviewExecutedHandler(myDataGrid, new ExecutedRoutedEventHandler(MyPreviewDeleteCommandHandler));

And define your command handler:

private void MyPreviewDeleteCommandHandler(object sender, ExecutedRoutedEventArgs e)
{
    if (e.Command == DataGrid.DeleteCommand)
    {
        if (!(MessageBox.Show("Are you sure?", "Confirm", MessageBoxButton.OKCancel) == MessageBoxResult.OK))
        {
            e.Handled = true; // Mark the command as handled to suppress the real command handler
        }
    }
}