none
Deleting multiple items in list

    Question

  • here is my code

    var g = main.SelectedIndex;
    myapp.totlist.RemoveAt(g);

    in the above code i was removing one item from myapp.totlist of type IList<object> but i wanted to delete multiple items selected in listview please help me

    Thanks in Advance
    Thursday, May 02, 2013 6:13 PM

Answers

  • Not sure whats going wrong at your end. It will be good if you can upload a small project with reproducible issue. 

    As a side note, we cannot remove conditions as those are checks to avoid crash and verify assumptions. Also look at ObservableCollection and use DataBinding, that it will be easier to keep data model and UI in sync.

    Here is code which works on my end -

    XAML

    <StackPanel Grid.Row="1">
            <Button Click="Button_RemoveSelectionClick" >Remove Selection</Button>
                <ListView Name="listview" SelectionMode="Multiple" IsItemClickEnabled="False" Margin="6">                
                </ListView>
            </StackPanel>

    CS

      IList<int> _totlist = null;
            protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
            {
                try
                {
                    // Dummy data
                    _totlist = new List<int>();
                    for (int i = 0; i < 9; ++i)
                    {
                        _totlist.Add(i + 1);
                    }
                    listview.ItemsSource = _totlist;
                }
                catch (Exception)
                {
                }
            }

     private void Button_RemoveSelectionClick(object sender, RoutedEventArgs e)
            {
                try
                {
                    for (int i = 0; i < listview.SelectedItems.Count; ++i)
                    {
                        int selItem = (int)listview.SelectedItems[i]; // cast will be of Type Item, in this case item type is int, so int                    
                        int itemIndex = listview.Items.IndexOf(selItem);
                        if (itemIndex != -1 && itemIndex < _totlist.Count)
                        {
                            _totlist.RemoveAt(itemIndex);
                        }
                    }
                    listview.ItemsSource = null;
                    listview.ItemsSource = _totlist;
                }
                catch (Exception)
                {
    
                }
            }


    Thanks,
    Sachin
    My Samples | Personal Website

    • Marked as answer by jayanth.k Saturday, May 04, 2013 9:34 AM
    Friday, May 03, 2013 5:46 PM

All replies

  • You can't remove multiple items in a single line.

    If you wan to remove first three items:

    listView.Items.RemoveAt(0);
    listView.Items.RemoveAt(0);
    listView.Items.RemoveAt(0);

    If you want to remove last three items:

    listView.Items.RemoveAt(listView.Items.Count - 1);
    listView.Items.RemoveAt(listView.Items.Count - 1);
    listView.Items.RemoveAt(listView.Items.Count - 1);

    Give us more details, what do you want to achieve or you're just curious if there is a workaround for this limitation.
    There is a technicall reason for this and it is easier to implement an custom items control with IList then fully featured List.

    Thursday, May 02, 2013 6:30 PM
  • actually i have one list of items i.e totlist and i wanted to remove the items which were selected in listview and in the above code main is the listview that contains totlist items
    Thursday, May 02, 2013 6:42 PM
  • Are you using binding? If so just update the list and your ListView should rebind and update...
    Thursday, May 02, 2013 6:48 PM
  • You want something like this?

    if (listView.SelectedItems != null)
    {
        var items = listView.SelectedItems.ToList();
        foreach (var item in items)
        {
            listView.Items.Remove(item);
        }
    }

    Thursday, May 02, 2013 6:51 PM
  • but actually my data was in list and i wanted remove them in list not in the listview items

    Thursday, May 02, 2013 7:01 PM
  • We cannot help you if you don't give us enough informations or what exactly you want to achieve.

    Give us information or code how is data and ListView related (do you add them manually or through data binding) and when do you want to remove them. (by pressing a button, when selecting, etc.)

    I want to help you but give me more informations or a working code which I can work on.

    Thanks.

    Thursday, May 02, 2013 7:09 PM
  • okay

    i have two pages one is items page an another is totalitemsselected page

    in items i collect the list of items select by user and append it to another list i.e totallist

    myapp.list = listitemsin.SelectedItems;
                    
                ((List<Object>)myapp.totlist).AddRange(myapp.list);

    in totalitemsselected page user can select the items to remove or clear the items and i am displaying items in a listview

    listview.ItemsSource = myapp.totlist;

    and if user selects some items in listview and clicks button it must delete the selected items

    var g = listview.SelectedIndex;
    myapp.totlist.RemoveAt(g);

    but this deletes only one item from selected items

    how can i implement to remove multiple items

    Thursday, May 02, 2013 7:22 PM
  • Try this - 

    for (int i = 0; i < listview.SelectedItems.Count; ++i)
                    {
                        ListViewItem selItem = listview.SelectedItems as ListViewItem;
                        if (selItem != null)
                        {
                            int itemIndex = listview.Items.IndexOf(selItem);
                            if (itemIndex != -1 && itemIndex < myapp.totlist.Count)
                                myapp.totlist.RemoveAt(itemIndex);
                        }
    
                    }



    Thanks,
    Sachin
    My Samples | Personal Website

    Friday, May 03, 2013 12:01 AM
  • i have tried it but its not working

    the items in myapp.totlist are not removing

    Friday, May 03, 2013 9:55 AM
  • please help me
    Friday, May 03, 2013 1:23 PM
  • Sorry there was typo in above code, try this one (lstView.SelectedItems[i] instead of lstView.SelectedItems)

     for (int i = 0; i < listview.SelectedItems.Count; ++i)
                    {
                        ListViewItem selItem = listview.SelectedItems[i] as ListViewItem;
                        if (selItem != null)
                        {
                            int itemIndex = listview.Items.IndexOf(selItem);
                            if (itemIndex != -1 && itemIndex < myapp.totlist.Count)
                                myapp.totlist.RemoveAt(itemIndex);
                        }
    
                    }


    Thanks,
    Sachin
    My Samples | Personal Website




    Friday, May 03, 2013 1:49 PM
  • no it was not working and i have removed the conditions in your code and it generated catostrophic error

    i  have tried this myapp.totlist.Remove(main.SelectedItems);

    but it was also not working

    Friday, May 03, 2013 4:52 PM
  • Not sure whats going wrong at your end. It will be good if you can upload a small project with reproducible issue. 

    As a side note, we cannot remove conditions as those are checks to avoid crash and verify assumptions. Also look at ObservableCollection and use DataBinding, that it will be easier to keep data model and UI in sync.

    Here is code which works on my end -

    XAML

    <StackPanel Grid.Row="1">
            <Button Click="Button_RemoveSelectionClick" >Remove Selection</Button>
                <ListView Name="listview" SelectionMode="Multiple" IsItemClickEnabled="False" Margin="6">                
                </ListView>
            </StackPanel>

    CS

      IList<int> _totlist = null;
            protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
            {
                try
                {
                    // Dummy data
                    _totlist = new List<int>();
                    for (int i = 0; i < 9; ++i)
                    {
                        _totlist.Add(i + 1);
                    }
                    listview.ItemsSource = _totlist;
                }
                catch (Exception)
                {
                }
            }

     private void Button_RemoveSelectionClick(object sender, RoutedEventArgs e)
            {
                try
                {
                    for (int i = 0; i < listview.SelectedItems.Count; ++i)
                    {
                        int selItem = (int)listview.SelectedItems[i]; // cast will be of Type Item, in this case item type is int, so int                    
                        int itemIndex = listview.Items.IndexOf(selItem);
                        if (itemIndex != -1 && itemIndex < _totlist.Count)
                        {
                            _totlist.RemoveAt(itemIndex);
                        }
                    }
                    listview.ItemsSource = null;
                    listview.ItemsSource = _totlist;
                }
                catch (Exception)
                {
    
                }
            }


    Thanks,
    Sachin
    My Samples | Personal Website

    • Marked as answer by jayanth.k Saturday, May 04, 2013 9:34 AM
    Friday, May 03, 2013 5:46 PM
  • Thanks Sachin it worked fine

    i made mistake in casting and atlast  it worked fine

    Once again thank you Sachin

    Saturday, May 04, 2013 9:40 AM
  • You’re Welcome

    Thanks,
    Sachin
    My Samples | Personal Website

    Saturday, May 04, 2013 6:28 PM