Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello, i use this code for delete multiple items from listview, but problem is that it delete only one item at a time.
so please help me what is use to delete multiple selected items.


C#
for (int i = listView2.SelectedItems.Count - 1; i >= 0; i++)
{
   if (listView2.Items[i].Selected)
   {
      listView2.Items.Remove(listView2.Items[i]);
   }
}

thanks in advance.
Posted
Updated 4-Apr-11 2:14am
v2
Comments
Sunasara Imdadhusen 4-Apr-11 8:14am    
Added coding format
BobJanova 4-Apr-11 10:24am    
Try i--

Try using BeginUpdate and EndUpdate to control the update of your ListView

C#
try
{
    listview2.BeginUpdate();
    foreach (ListViewItem item in this.listview2.SelectedItems)
    {
        this.listview2.Items.Remove(item);
    }
}
finally
{
    this.listview2.EndUpdate();
}
 
Share this answer
 
Comments
#realJSOP 4-Apr-11 9:48am    
5 - Proposed as answer
you can also try it
SQL
while (listView2.SelectedItems.Count > 0)
     {
         listView2.Items.Remove(listView2.SelectedItems[0]);
     }
 
Share this answer
 
v2
C#
foreach (ListViewItem ListItem in listView1.Items)
 {
      if (ListItem.Selected == true)
            listView1.Items.Remove(ListItem);
 }
 
Share this answer
 
Comments
BobJanova 4-Apr-11 10:25am    
Won't this cause an 'Enumeration was modified' exception to be thrown?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900