user913213
user913213

Reputation: 21

Listview slow refresh in c#

I have one ArrayList and it changes often.

The ListView displays the ArrayList's data so this ListView must be quickly changed when the ArrayList has changed.

So I wrote code like this:

ArrayList ar;
ListView lv;

paintmethod()
{
  lv.items.clear();
  lv.addlistview(ar);
  lv.invalidate();
}

private void addlistview(ArrayList arr)
{
  lv.Items.Add(arr.somedata.tostring(),arr.somedata.tostring());
}

This code works but when the ArrayList has changed ListView is not refreshed immediately.

It's refreshed 20secs, 30secs or even 1 minute later.

How can I do more to solve this problem?

Upvotes: 2

Views: 2843

Answers (3)

CharithJ
CharithJ

Reputation: 47610

Try below and see any better. It's good practice to use BeginUpdate() and EndUpdate() when updating multiple listView items. Because BeginUpdate() prevents the control from drawing until the EndUpdate method is called.

paintmethod()
{
    lv.BeginUpdate();

    lv.items.clear();
    lv.addlistview(ar);

    lv.EndUpdate();
}

The preferred way to add multiple items to a ListView is to use the AddRange method of the ListView.ListViewItemCollection (accessed through the Items property of the ListView). This enables you to add an array of items to the list in a single operation.

MSDN

This should speeds up performance a lot.

Upvotes: 4

Josh C.
Josh C.

Reputation: 4383

Is there are reason you are not binding your listview.ItemsSource to an observablecollection? Then, you would only need to work against the observable collection, and that would notify the listview incremently.

Upvotes: 1

becike
becike

Reputation: 375

this.SuspendLayout();
lv.items.clear();
lv.addlistview(ar);
lv.invalidate();
this.ResumeLayout(false);

Upvotes: 0

Related Questions