Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 165451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:55:24+00:00 2026-05-11T11:55:24+00:00

I want to enable the user to highlight a row on the WPF DataGrid

  • 0

I want to enable the user to highlight a row on the WPF DataGrid and press delete key to delete the row.

  • the functionality is already built into the UI of the grid, so to the user, the row disappears
  • I currently handle this on the SelectionChanged event (code below)
  • I loop through all the ‘e.RemovedItems’ and delete them with LINQ

Problem is: even when you simply select a row and move off of it, selection change is fired and that row is in e.RemovedItems (which is odd, why would simply selecting something put it in a RemovedItems container?).

So I am looking for a DeleteKeyPressed event so I can simply handle it. What is that event called?

I am using the March 2009 toolkit.

XAML:

<Grid DockPanel.Dock='Bottom'>     <toolkit:DataGrid x:Name='TheDataGrid'                        SelectionChanged='TheDataGrid_SelectionChanged'                       AutoGenerateColumns='True'                       RowEditEnding='TheDataGrid_RowEditEnding'/> 

code-behind:

private void TheDataGrid_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) {     if (e.RemovedItems.Count > 0)     {         Message.Text = 'The following were removed: ';         foreach (object obj in e.RemovedItems)         {             Customer customer = obj as Customer;             Message.Text += customer.ContactName + ',';             _db.Order_Details.DeleteAllOnSubmit(                 customer.Orders.SelectMany(o => o.Order_Details));             _db.Orders.DeleteAllOnSubmit(customer.Orders);             _db.Customers.DeleteOnSubmit(customer);         }      }      try     {         _db.SubmitChanges();     }     catch (Exception ex)     {         Message.Text = ex.Message;     } } 

ANSWER:

Thanks lnferis, that was exactly what I was looking for, here is my finished delete handling event for the datagrid, note the KeyDown event doesn’t fire for some reason.

XAML:

<toolkit:DataGrid x:Name='TheDataGrid'                    KeyDown='TheDataGrid_KeyDown'                   PreviewKeyDown='TheDataGrid_PreviewKeyDown'                   AutoGenerateColumns='True'                   RowEditEnding='TheDataGrid_RowEditEnding'/> 

code-behind

private void TheDataGrid_PreviewKeyDown(object sender, KeyEventArgs e) {     if (e.Key == Key.Delete)     {         var grid = (DataGrid)sender;          if (grid.SelectedItems.Count > 0)         {             string checkMessage = 'The following will be removed: ';              foreach (var row in grid.SelectedItems)             {                 Customer customer = row as Customer;                 checkMessage += customer.ContactName + ',';             }             checkMessage = Regex.Replace(checkMessage, ',$', '');              var result = MessageBox.Show(checkMessage, 'Delete', MessageBoxButton.OKCancel);             if (result == MessageBoxResult.OK)             {                 foreach (var row in grid.SelectedItems)                 {                     Customer customer = row as Customer;                     _db.Order_Details.DeleteAllOnSubmit(                         customer.Orders.SelectMany(o => o.Order_Details));                     _db.Orders.DeleteAllOnSubmit(customer.Orders);                     _db.Customers.DeleteOnSubmit(customer);                 }                 _db.SubmitChanges();             }             else             {                 foreach (var row in grid.SelectedItems)                 {                     Customer customer = row as Customer;                     LoadData();                     _db.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, customer); //TODO: this doesn't refresh the datagrid like the other instance in this code                 }             }         }     } }  private void TheDataGrid_KeyDown(object sender, KeyEventArgs e) {     Console.WriteLine('never gets here for some reason'); } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T11:55:25+00:00Added an answer on May 11, 2026 at 11:55 am

    The RemovedItems items reflects the items removed from the selection, and not from the grid.

    Handle the PreviewKeyDown event, and use the SelectedItems property to delete the selected rows there:

    private void PreviewKeyDownHandler(object sender, KeyEventArgs e) {     var grid = (DataGrid)sender;     if ( Key.Delete == e.Key ) {         foreach (var row in grid.SelectedItems) {             ... // perform linq stuff to delete here         }     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to enable an UIAlertView-Request before the user can delete an item from
I want to enable an user to be able to communicate with other users
I want to enable the user to select a video file from the on-device
In my app the user generates text content. I want to enable the user
I simply want to enable the user of my web site to change the
I want to enable an effect on my web app where a user clicks
I want to enable the user to drag a bar up/down to reveal/hide content
As the title says really. I want to enable sharing user generated content easily
In my Activity when the user says they want to enable notifications I call
I have the following javascript, which I want to use to enable the user

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.