Adding lots of item, very slow Listview

2 posts, 0 answers
  1. Matheo
    Matheo avatar
    1 posts
    Member since:
    Jun 2012

    Posted 15 Feb 2013 Link to this post

    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
  2. Ivan Petrov
    Admin
    Ivan Petrov avatar
    701 posts

    Posted 20 Feb 2013 Link to this post

    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.
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top