Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding huge number of items to a Listview

Tags:

c#

.net

winforms

I have a listview, which is used as an index to a simple search application. Each item of the index is a word, and clicking on the item will add that item to the search textbox. The user can first click on any of the words she/he prefers to and them to the search textbox and then click search, to search in the documents. The problem is that adding more than around 1000 items to a ListView takes a lot of runtime! I have designed a progressbar and added a timer which starts adding items to the listview as soon as the form loads. This gives responsiveness to the application, but still the efficiency is very low. I suspect there might be around 100,000 words in the index when the document base grows enough so I need a more efficient way to do this. Maybe I need to change the ListView component to something else. This is the code in the timer to add the items to the listview:

if (!listViewDone)
        {
            int pos = 0;
            ListView listView1 = Search.getInstance().getListView();
            listView1.BeginUpdate();
            for (pos = listViewPos; pos < termf.Count && pos < listViewPos + listViewChunk; ++pos)
            {
                TermFreq t = termf[pos];
                listView1.Items.Add(new ListViewItem(new String[] { t.term }));
                progressBar1.Value = pos;
            }
            listView1.EndUpdate();
            listViewPos = pos;

            if (pos == termf.Count)
            {
                listViewDone = true;
                termf = null;
                timer1.Enabled = false;
                Visible = false;
            }
        }
like image 603
Mostafa Mahdieh Avatar asked Jun 29 '10 04:06

Mostafa Mahdieh


3 Answers

As hmemcpy mentioned, the VirtualMode will speed things up considerably. I'm not sure about the commercial nature of the project but I have used the excellent open source ObjectListView which includes a FastObjectListView variant.

This is basically an extended virtual ListView that is extremely fast and as an added bonus is much nicer to work with. The documentation claims it "can build a list of 10,000 objects in less than 0.1 seconds" and while I can't vouch for that whenever I have used it I've never noticed any serious lag. The licensing could be an issue for you though.

like image 137
Lummo Avatar answered Oct 06 '22 00:10

Lummo


Don't use a list-view, it wasn't meant to handle so many items and even with perfect performance will annoy your users. Consider using an auto-complete textbox instead.

like image 20
Jonathan Allen Avatar answered Oct 06 '22 02:10

Jonathan Allen


If you need to display that many items in a ListView, your best option is using the ListView's Virtual Mode. This way your ListView will only display visible items.

like image 41
Igal Tabachnik Avatar answered Oct 06 '22 01:10

Igal Tabachnik