views:

1344

answers:

4

I realize i have to sort the collection where the ListView gathers the items from:
ListView listCollection = new ListView();

But this doesn't seem to work unless the ListView is added as a GUI-control to the form, that in turn makes it very slow to add items to, hence why i have to use VirtualMode on my GUI-ListView in the first place.

Anyone know how to go about this or point me in the right direction?

+1  A: 

Did you try beginupdate() and endupdate()? Adding data is much faster when you use beginupdate/endupdate.(when you call beginupdate, listview doesn't draw until you call endupdate)

listView1.BeginUpdate();
for (int i = 0; i < 20000; i++)
{
listView1.Items.Add("abdc", 1);
}
listView1.EndUpdate();
barism
Does not help app still hangs when adding maaany items.
You can create a thread for adding items.(or call processmessages() after every Items.Add() but threads are far better than processmessages)
barism
+3  A: 

basically, you will need to apply sort to the data pump itself.

I did a quick search on Google for listview sort virtualmode. First result was this page, where the above quote was taken from.

For example, if your datasource is a DataView, apply sorting on this instead of the ListView.

If it is just a question of performance when adding items, I would do as barism suggests; use BeginUpdate/EndUpdate instead of VirtualMode.

try {
  listView1.BeginUpdate();
  // add items
}
finally {
  listView1.EndUpdate();
}
Bernhof
Program still hangs, i must use VM because the list can become very large (50k+ items)
Well, in that case (if you absolutely need to load 50k+ items) I would store the items in a list of some kind, and sort the items there (as mentioned, you need to apply the sort to the data pump itself), though you'll obviously encounter some delay when sorting this many items, unless you find some super-sorting algorithm that I'm not familiar with :-) (which is not completely unthinkable)
Bernhof
+2  A: 

If you are using virtual mode, you have to sort your underlying data source. As you may have found, ListViewItemSorter does nothing for virtual lists.

If you are using a non-virtual listview, you can also use AddRange(), which is significantly faster than a series of Add() -- In addition to using BeginUpdate/EndUpdate that has already been described.

ObjectListView (an open source wrapper around .NET WinForms ListView) already uses all these techniques to make itself fast. It is a big improvement over a normal ListView. It supports both normal mode and virtual mode listviews, and make them both much easier to use. For example, sorting is handled completely automatically.

Grammarian
A: 

For more details please visit ListView In Virtual Mode Sorting

bimbim.in