none
Datagrid doesn't delete row on delete key press RRS feed

  • Question

  • I made a WPF application and had a datagrid bound to a List<Table>.  I displayed the Name property of the table for each row. I used the CanUserDeleteRows option.  When I pressed the delete key, the row was deleted not only from the display, but my list.

    Now I have refactored and I have a Dictionary<String,Table> instead.  I am displaying the Value.Name.

    The Datagrid is displaying the proper information, but now when I press the delete key, nothing happens.

    Is that how it is supposed to work, or am I missing something?

    If I have to code the delete myself, how do I make that happen?

    Wednesday, October 19, 2016 1:34 PM

Answers

  • As I said, I'll just use the List. Then I don't have to make a command or handle an event or anything. And I don't have to have code behind that knows what kind of thing it is bound to.

    It did have something to do with the original question because with List, I don't have to do anything for my list to be modified in my Viewmodel when the Delete key is pressed and I was hoping there was some way that a Dictionary would work with my Viewmodel also.  I'm not entirely sure why the DataGrid can't know it has a Dictionary and to delete the item from the Dictionary just like it could delete the String from the List.

    Anyway, my solution is to just stick to the List since apparently other options mess up my clean separation of concerns unless I go to more effort than I want to spend on it.


    Thanks anyway.
    • Edited by Mark Tiede Wednesday, October 26, 2016 3:29 PM
    • Marked as answer by Mark Tiede Monday, October 31, 2016 10:38 AM
    Wednesday, October 26, 2016 3:28 PM

All replies


  • Hi Mark Tiede,

    >>The Datagrid is displaying the proper information, but now when I press the delete key, nothing happens.

    As for the delete-key, this really requires code specific to the DataGrid control and attach to the DG's PreviewKeyDown-event. You can refer the following code.

     private void DGrid_PreviewKeyDown(object sender, KeyEventArgs e)
            {
                if (e.Key == Key.Delete)
                {
                    System.Windows.Controls.DataGrid dg = (System.Windows.Controls.DataGrid)sender;
                    if (dg != null)
                    {
                        // User is attempting to delete the row
                        var result = MessageBox.Show(
                            "About to delete the current row.\n\nProceed?",
                            "Delete",
                            MessageBoxButton.YesNo,
                            MessageBoxImage.Question,
                            MessageBoxResult.No);
                        e.Handled = (result == MessageBoxResult.No);
                    }
                }
            }


    The following article for your reference.

    WPF Datagrid Update, Insert and Delete Operations:

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.


    Best Regards,

    Yohann Lu

    Thursday, October 20, 2016 6:00 AM
    Moderator
  • Yohann,  

    Your reply doesn't seem to address my problem.  Your example is just showing hooking into the keydown and prompting whether to delete or not.  That would be useful if you wanted to prompt.  I just want to delete.  If I went to the effort to put that code in, it still wouldn't delete because it doesn't delete WITHOUT the code.

    The problem is that when the ItemsSource is a List, the datagrid seems to know what to do to delete the item.  But if it is a Dictionary, it doesn't seem to work.

    I switched back to a List and will dynamically convert it to a dictionary when I need a lookup.

    Thursday, October 20, 2016 12:13 PM
  • >>Is that how it is supposed to work ...

    Yes. The type of the source collection matters. Please refer to the following links for more information about this:

    Can't remove records from DataGrid when it binded to Dictionary<,>: http://stackoverflow.com/questions/7595548/cant-remove-records-from-datagrid-when-it-binded-to-dictionary
    Overview of the editing features in the WPF DataGrid: https://blogs.msdn.microsoft.com/vinsibal/2008/10/01/overview-of-the-editing-features-in-the-wpf-datagrid/

    >>If I have to code the delete myself, how do I make that happen?

    You could handle the KeyDown event as suggested above and delete the key from the Dictionary like this:

    private void Dgg_PreviewKeyDown(object sender, KeyEventArgs e)
            {
                DataGrid dg = sender as DataGrid;
                var pair = (KeyValuePair<string, Table>)dg.SelectedItem;
                (dg.ItemsSource as Dictionary<string, Table>).Remove(pair.Key);
                dg.Items.Refresh();
            }

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Friday, October 21, 2016 8:42 PM
  • Because I'm using MVVM, I won't want the code behind to know about the SelectedItem being a KeyValuePair. I just switched it back to a List and I make Dictionary from the List when I need it.
    Monday, October 24, 2016 2:43 PM
  • >>Because I'm using MVVM, I won't want the code behind to know about the SelectedItem being a KeyValuePair.

    This doesn't really has anything to do with your original question but you could invoke a command when the PreviewKeyDown event is raised using an interaction trigger. Please refer to my blog posts about how to handle events in MVVM for more information about this: https://blog.magnusmontin.net/2013/06/30/handling-events-in-an-mvvm-wpf-application/.

    But if you are serious about using the MVVM pattern you should not bind to a System.Collection.Generic.Dictionary but to a collection that implements the INotifyCollectionChanged interface and raises notifications when an item is added and removed: https://msdn.microsoft.com/en-us/library/system.collections.specialized.inotifycollectionchanged(v=vs.110).aspx

    WPF provides the ObservableCollection<T> class that does this: https://msdn.microsoft.com/en-us/library/system.collections.specialized.inotifycollectionchanged(v=vs.110).aspx

    Please remember to close this thread by marking all helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Monday, October 24, 2016 4:40 PM
  • As I said, I'll just use the List. Then I don't have to make a command or handle an event or anything. And I don't have to have code behind that knows what kind of thing it is bound to.

    It did have something to do with the original question because with List, I don't have to do anything for my list to be modified in my Viewmodel when the Delete key is pressed and I was hoping there was some way that a Dictionary would work with my Viewmodel also.  I'm not entirely sure why the DataGrid can't know it has a Dictionary and to delete the item from the Dictionary just like it could delete the String from the List.

    Anyway, my solution is to just stick to the List since apparently other options mess up my clean separation of concerns unless I go to more effort than I want to spend on it.


    Thanks anyway.
    • Edited by Mark Tiede Wednesday, October 26, 2016 3:29 PM
    • Marked as answer by Mark Tiede Monday, October 31, 2016 10:38 AM
    Wednesday, October 26, 2016 3:28 PM

  • Hi Mark Tiede,

    >>  I'm not entirely sure why the DataGrid can't know it has a Dictionary and to delete the item from the Dictionary just like it could delete the String from the List.

    As far as I now, you need to add code manually to implement delete records. I suggest you can try the Magnus's suggestions, He gave us a right direction.

    I hope you can achieve your needs as soon as possible.


    Best Regards,

    Yohann Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 31, 2016 7:40 AM
    Moderator