This is a migrated thread and some comments may be shown as answers.

Adding lots of item, very slow Listview

1 Answer 1136 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Matheo
Top achievements
Rank 1
Matheo asked on 15 Feb 2013, 02:55 PM
Hello,
I have a problem when I want to add a lot of item (with picture) has both in a listview.
In fact, I have a loop that adds about 400 items, but when I run this loop, the listview takes a long time to fill.
I tried my code with a winform listview and there is no problem, items are added instantly.
Here is my code :

List<ListViewDataItem> items = new List<ListViewDataItem>();
private
void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            int nb = 0;
            foreach (ListViewDataItem item in items)
            {
                item.ImageIndex = nb;
                this.Invoke(new AddItem(AddItem2), item);
               nb += 1;
            }
            lblStatus.Text = "Chargement terminé";
        }
        private delegate void AddItem(ListViewDataItem itm);
        private void AddItem2(ListViewDataItem itm)
        {
            radListView1.Items.Add(itm);
            radListView1.Refresh();
        }
Sorry for my english,I'm French
Thanx

1 Answer, 1 is accepted

Sort by
0
Ivan Petrov
Telerik team
answered on 20 Feb 2013, 02:42 PM
Hello Matheo,

Thank you for writing.

There are several things that need improvement in the code snippet you have sent:

1. The RunWorkerCompleted event handling method is executed in the calling thread which in your case is the main UI thread. That said there is no need to invoke the method that adds items to RadListView, as Invoke is intended for cross thread access. Additionally Invoke is a costly function.

2. In the method that adds items to list view you call the Refresh method of RadListView. This forces the control to invalidate its client area and redraw itself. This again is a very costly method which should be called, if at all, after a batch of updates to the control.

3. To maximize the performance of RadListView while adding items you should surround the code block that adds them with a BeginUpdate() EndUpdate() method calls.

Here is how your code might look like with all the above improvements:
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    this.radListView1.BeginUpdate();
 
    int nb = 0;
 
    foreach (ListViewDataItem item in items)
    {
        item.ImageIndex = nb;
        radListView1.Items.Add(item);
        nb += 1;
    }
 
    this.radListView1.EndUpdate();
 
    lblStatus.Text = "Chargement terminé";
}
I hope this will be useful. Should you need further assistance, I would be glad to provide it.

All the best,
Ivan Petrov
the Telerik team
Q3'12 SP1 of RadControls for WinForms is out now. See what's new.
Tags
ListView
Asked by
Matheo
Top achievements
Rank 1
Answers by
Ivan Petrov
Telerik team
Share this question
or