pedja
pedja

Reputation: 3413

Adding items in ListView too slow

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?

Upvotes: 7

Views: 8007

Answers (3)

Matt
Matt

Reputation: 224

It could be the string indexer lookups causing the slow down. Can you count on the indexes being static? You might try replacing the code with integer indexes and see if that improves performance.

Upvotes: 0

Yatrix
Yatrix

Reputation: 13775

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

Upvotes: 10

Justin
Justin

Reputation: 3397

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.

Upvotes: 1

Related Questions