Efficient way of removing selected items from a (very) large ListView
- From: "Bry" <bryanhobson@xxxxxxxxx>
- Date: 17 Jan 2007 07:43:46 -0800
I have a large list view item containing many thousands of
ListViewItems, and I need to be able to remove all the selected items.
(The number of selected items could also be many thousands)
The following code works, but is very slow:
foreach (ListViewItem lvi in lv.SelectedItems)
lv.Items.Remove(lvi);
An alternative approach that would probably be quicker is something
like this:
foreach (int index in lv.SelectedIndices)
lv.Items.RemoveAt(index);
but this won't work because all the indexes would be incorrect once the
first item has been removed. If the foreach loop always gives me the
integers in ascending order, then I could work around the problem like
this:
int loop = 0;
foreach (int index in lv.SelectedIndices)
lv.Items.RemoveAt(index-(loop++));
Does anyone know if a foreach loop would iterate the objects in index
order?, alternatively, I could sort the index values into descending
order, then perform:
lv.Items.RemoveAt(index);
on each item in the sorted list, but is there a better (read:
'correct') way of doing this?
.
- Follow-Ups:
- Re: Efficient way of removing selected items from a (very) large ListView
- From: Bryan Phillips
- Re: Efficient way of removing selected items from a (very) large ListView
- From: JerryWEC
- Re: Efficient way of removing selected items from a (very) large ListView
- Prev by Date: Listview Control ?
- Next by Date: Re: AutoComplete ComboBox with DroppedDown=true
- Previous by thread: Listview Control ?
- Next by thread: Re: Efficient way of removing selected items from a (very) large ListView
- Index(es):