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.
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.
5 Answers, 1 is accepted
0
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:
I hope this helps.
Greetings,
Stefan
the Telerik team
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.
0
Kamelia
Top achievements
Rank 1
answered on 16 Sep 2014, 02:12 PM
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
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
0
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
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.
0
Kamelia
Top achievements
Rank 1
answered on 23 Sep 2014, 02:44 PM
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
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
0
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
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.