ListView -DetailView item removing

6 posts, 0 answers
  1. John Mark
    John Mark avatar
    1 posts
    Member since:
    Feb 2013

    Posted 10 Feb 2013 Link to this post

    how to remove items from listview?
    i tried this pieces of code but i got an exception
    foreach (ListViewDataItem eachItem in radListView1.SelectedItems)
                {
                    radListView1.Items.Remove(eachItem);
                } 

    my exception is Collection was modified; enumeration operation may not execute.
  2. Stefan
    Admin
    Stefan avatar
    2890 posts

    Posted 13 Feb 2013 Link to this post

    Hi John,

    Thank you for writing.

    When you delete an items it gets deleted from the SelectedItems collection as well, and this is why you get this exception. To safely delete them, you need to copy the items in a list and then iterate your list and delete them:
    foreach (ListViewDataItem eachItem in radListView1.SelectedItems.ToList())
    {
          radListView1.Items.Remove(eachItem);
    }
     
    //or
     
    List<ListViewDataItem> saveSelectedItems = new List<ListViewDataItem>();
     
    foreach (ListViewDataItem eachItem in radListView1.SelectedItems)
    {
        saveSelectedItems.Add(eachItem);
    }
     
    foreach (ListViewDataItem item in saveSelectedItems)
    {
        radListView1.Items.Remove(item);
    }

    I hope this helps.
     

    Greetings,
    Stefan
    the Telerik team
    Q3'12 SP1 of RadControls for WinForms is out now. See what's new.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Kamelia
    Kamelia avatar
    7 posts
    Member since:
    Sep 2014

    Posted 16 Sep 2014 in reply to Stefan Link to this post

    hi, I got this error too! :(
    and I tried your solution
    but again I got an error in foreach part :

    cannot convert type 'Telerik.wincontrols.UI.RadListDataItem' to 'Telerik WwinControls.UI.ListviewDataItem'


    pleeeeeaaaase help me 
    thanks
  5. Stefan
    Admin
    Stefan avatar
    2890 posts

    Posted 16 Sep 2014 Link to this post

    Hello Kamelia,

    I assume that you are using RadListControl or RadDropDownList, rather than RadListView, which this thread concerns. If so, in your foreach loop, you need to use RadListDataItem instead of ListViewDataItem, as the former is the type of the data items in RadListControl/RadDropDownList.

    I hope this helps.

    Regards,
    Stefan
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  6. Kamelia
    Kamelia avatar
    7 posts
    Member since:
    Sep 2014

    Posted 23 Sep 2014 in reply to Stefan Link to this post

    thank you so much 

    I have another question about RadGrieview


    I want the color of any rows which was selected before ., changes 
    in a way that by looking at my RadGridview , I can see the which of the rows has been selected ones before!
    thank you in advance
  7. Stefan
    Admin
    Stefan avatar
    2890 posts

    Posted 24 Sep 2014 Link to this post

    Hi Kamelia,

    This forum concerns RadListView, not RadGridView. I have answered your grid view question in the grid view forum: http://www.telerik.com/forums/change-selected-grid-view-row-color.

    May I please ask you to avoid posting unrelated questions in other threads. If you cannot find a thread for the particular case you have, please open a new one. This way we can keep the forums clean and easy to navigate.

    Thank you for the understanding.

    Regards,
    Stefan
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
Back to Top
UI for WinForms is Visual Studio 2017 Ready