Re: Efficient way of removing selected items from a (very) large ListView
- From: "Bryan Phillips" <bphillips@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 18 Jan 2007 02:33:58 +0000
This should work:
for(int i=listView1.SelectedIndices.Count-1; i>=0; i--){
listView1.Items.RemoveAt(listView1.SelectedIndices[i]);
}
--
Bryan Phillips
MCSD, MCDBA, MCSE
Blog: http://bphillips76.spaces.live.com
"Bry" <bryanhobson@xxxxxxxxx> wrote in message news:1169048626.083431.233220@xxxxxxxxxxxxxxxxxxxxxxxxxxxx:
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?
.
- References:
- Prev by Date: Re: ListBox in Windows App
- Next by Date: Re: AutoComplete ComboBox with DroppedDown=true
- Previous by thread: Re: Efficient way of removing selected items from a (very) large ListView
- Next by thread: Re: DataGrid Hierarchical Data view display in Windows Form
- Index(es):
Loading