Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding items in ListView too slow

Tags:

c#

winforms

I have a ListView and i add items to it one-by-one with a loop
This ListView has CheckBoxes

In that loop i decide whether checkbox should be checked or not
Problem is if too many checkboxes should be checked adding items is too slow
Here is the code:

for (int i = 0; i < dt.Rows.Count; i++)
{
    DataRow drow = dt.Rows[i];

    // Only row that have not been deleted
    if (drow.RowState != DataRowState.Deleted && int.Parse(drow["season"].ToString()) != 0)
    {
        ListViewItem lvi = new ListViewItem(drow["episode_name"].ToString());

        lvi.SubItems.Add(drow["first_aired"].ToString());
        lvi.SubItems.Add(drow["episode"].ToString());
        lvi.SubItems.Add(drow["season"].ToString());
        lvi.SubItems.Add(drow["rating"].ToString());
        lvi.SubItems.Add(drow["episode_id"].ToString());
        if (bool.Parse(drow["watched"].ToString()))
        {
            lvi.Checked = true; //this is the problem, when i remove it, adding is fast
        }
        else {
            lvi.Checked = false;
        }
        episodesList.Items.Add(lvi);
    }
}

How can i make it faster?

like image 267
pedja Avatar asked May 02 '13 17:05

pedja


2 Answers

On your listview, call .BeginUpdate() while you're loading in the results. After you're finished, call .EndUpdate(). This should speed it up as it's not trying to process and draw at the same time.

// wait to draw
episodesList.BeginUpdate();

// add your items

// draw everything
episodesList.EndUpdate();

EDIT

As Justin suggested, AddRange() could help some, but it probably won't eliminate your problem. See: How to speed adding items to a ListView?

EDIT #2

Since the event handlers are causing the issue, you can work around them by removing the handlers during load and re-adding them after. Or, you can use a global boolean _isLoading that you set before your load and after and then you can check _isLoading in your handlers so you don't make that trip to the Db. Removing/Readding the handler is probably the cleaner solution, though.

Removing event handlers

like image 194
Yatrix Avatar answered Sep 24 '22 01:09

Yatrix


Normally when you are making a large change, you should put mylistview.BeginUpdate() before your loop then mylistview.EndUpdate(). This will stop the list from updating until you have called the EndUpdate().

The Check box is probably causing the list to be redrawin slowing down the already large amount of work it has to do.

You could possibly try creating the list view items first then adding them to the listview using the AddRange() method. MSDN lists it as the perferred way.

like image 35
Justin Avatar answered Sep 20 '22 01:09

Justin