WPF DataGrid Remove SelectedItems

21,324

Solution 1

By removing selected item you are changing SelectedItems collection. You should copy it first and then start removing.

Solution 2

This also worked well for me.

while (dataGrid1.SelectedItems.Count > 0){
  dataGrid1_item_source.Rows.RemoveAt(dataGrid1.SelectedIndex);
}

Solution 3

The mistake you are doing here you are removing items during loop whaich is messing with loop count so make a copy grid and remove selecteditem from it and then equlize it by the orignal one.. Check this out

var grid = dataGrid1;
var mygrid = dataGrid1
if (grid.SelectedIndex >= 0)
{
  for (int i = 0; i <= grid.SelectedItems.Count; i++)
  {
    mygrid .Items.Remove(grid.SelectedItems[i]);  
   };
}

grid = mygrid;
Share:
21,324
Schahriar SaffarShargh
Author by

Schahriar SaffarShargh

I am a back-end developer living in Silicon Valley with a love of NodeJS and C++. Mix them together and you get the key to the gates of heaven. I develop servers and server-side applications on a day to day basis. Anything from a full-fledged SMTP server known as Galleon to a crazy firewall written in NodeJS. If you are interested to find more of my projects you can visit my Github or NPM page. Most of my projects are open-source. My large-scale production projects include a Stock Market Simulation that is run as a challenge (Stock Challenge) on a yearly basis in South East Asia, complete Mail Server available in private beta at galleon.email and the upcoming Guide &amp; Tutorial PaaS developed to be used for educational purposes initially planned to be deployed in the University of the West of England by the end of 2015. My past experiences include PHP (yeah I was a PHP guy), C#, C, Delphi, Java (academic) and a whole lot of front-end development.

Updated on July 05, 2022

Comments

  • Schahriar SaffarShargh
    Schahriar SaffarShargh almost 2 years

    Recently I've been working on a project which imports data programmicaly into a WPF DataGrid.

    I'm almost done with the project but the thing that I left out was a button to remove selected cells and this is where I'm stuck!

    I wrote this code using my basic knowledge of DataGrids:

    var grid = dataGrid1;
    if (grid.SelectedIndex >= 0)
     {
       for (int i = 0; i <= grid.SelectedItems.Count; i++)
       {
          grid.Items.Remove(grid.SelectedItems[i]);
       };
     }
    

    Works fine on removing only the item selected just like CurrentItem but it doesn't remove anymore than 2 selected items!

    The DataGrid I have should at least contain a minimum of 100 items. I've added a remove all option but this is also necessary.

    I'll be thankful if anyone gives me the solution.

  • Schahriar SaffarShargh
    Schahriar SaffarShargh almost 13 years
    Tnx! I copied SelectedItem object into a new object and added to the loop.
  • Rick Sladkey
    Rick Sladkey almost 13 years
    This does nothing because grid and mygrid are the same DataGrid.
  • Doug
    Doug over 10 years
    If you sorted the DataGrid, his does not work because the indices are different than the underlying item source. Also, it would be dataGrid1_item_source.RemoveAt(dataGrid1.SelectedIndex); Rows is not in an ObservableCollection.
  • Alexander
    Alexander over 5 years
    While this code may answer the question, providing additional context regarding how and why it solves the problem would improve the answer's long-term value.