Caching the Delete event in Datagrid

Apr 1, 2009 at 5:15 PM

Hi..

I'm trying to catch the delete event in the datagrid in order to display a message..

How can this be done?

I tried in XAML:

CommandManager.Executed="OnDelete"

Code Behind:

OnDelete(object sender, ExecutedRoutedEventArgs e)
{
    RoutedCommand routedCommand = (e.Command as RoutedCommand);
    if (routedCommand != null)
    {
        if (routedCommand.Name == "Delete")
        {
              MessageBox.Show("Deleted");

        }
    }
}



But it doesn't call the event...

I'm I missing something?

Thanks in advance
Apr 2, 2009 at 5:55 PM
Can anybody help me with this?

Is there another way to do it?

send a message everytime the user deletes a row (I can't find the event in the events list)

Thanks in advance
Apr 7, 2009 at 9:40 AM
Hy..

Sorry for being pushy.. but I really need this functionality...

Will it exist in the next version of WPF datagrid?

Is is already contemplated?  .. Something like OnUserDeletedRows event?

thanks in advance
Coordinator
Apr 7, 2009 at 8:15 PM
You cant use the CommandBinding because if you handle the command the default handler will not be executed (and hence the delete will not happen).

Handle PreviewKeyDown on DataGrid as follows...

        private void DataGrid_Standard_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Handled)
            {
                return;
            }

            if (e.Key == Key.Delete)
            {
                MessageBox.Show("Deleting the row");
                //dont set the e.Handled to true here.
            }
        }

Apr 9, 2009 at 8:41 AM
Thanks for your input brandf,

I tried it, but still found a problem:

When the user is edditing data on a cell and presses the Delete key, the same message appears... and in this case it should not...

Do you have a workaround for this?

Thanks in advance

PS: I really think it should be included a OnUserDeleteRow event...
Apr 16, 2009 at 10:03 AM

Can anyone please help?...

This very important for my project...

Are there steps being made to contemplate this in future versions?

Thanks in advance
Apr 18, 2009 at 10:13 PM

I override the DataGrid’s Delete command and do the delete myself. I do this by adding command binding on the DataGrid like this:

    <CommandBinding Command="wpft:DataGrid.DeleteCommand" Executed="DeleteTitleRow"/>

Where “wpft:” is define as xmlns:wpft="http://schemas.microsoft.com/wpf/2008/toolkit"  

 

Apr 19, 2009 at 3:03 PM
change the if statemant to:

if

 

(e.Key == Key.Delete && !e.OriginalSource.GetType().Equals(typeof(TextBox)))

This way you can prevent Delete key from textbox (editing cell mode)

 

Apr 21, 2009 at 10:02 PM
Thanks for your inputs..

I went with jonas_man idea.... but still missing a bit... basically upon data validation (which works a bit erraticly) and because I also have other columns like the chekbox column (which, in this case won't work...)...

Is there a way to work around it?

Thanks in advance
May 13, 2009 at 12:01 PM

I had a similiar problem. You should be able to do something like this:

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)
    {
        Console.Writeline("Delete...");
        // If you set e.Handled=true here, the delete command will be suppressed..
    }
}

This method will not be triggered when using the delete key while editing.

May 15, 2009 at 11:48 AM

Thanks oskarh...

It really worked great...

Many thanks.. Problem solved!!!

Oct 22, 2009 at 7:34 AM
oskarh wrote:

I had a similiar problem. You should be able to do something like this:

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)
    {
        Console.Writeline("Delete...");
        // If you set e.Handled=true here, the delete command will be suppressed..
    }
}

This method will not be triggered when using the delete key while editing.

And for people who prefer to attach event-handlers in XAML, you can assign the above handler (MyPreviewDeleteCommandHandler) declaratively by using xaml of the form:

<tk:DataGrid x:Name="MyDataGrid" CommandManager.PreviewExecuted="MyPreviewDeleteCommandHandler">

This is the same as oskarh's solution, but done in xaml instead of in the code-behind in the constructor.