Question

I have a TListView that gets populated with data collected over a network. To collect all the data takes around 50ms, to add it to the list takes around 5 seconds. My initial guess was that it was redrawing after every addition or something like that. What should I do to get the TListView to update as quickly as possible?

Columns and items are all added via code.

I tried using BeginUpdate and EndUpdate on the items of the list but that didn't make much difference. There are around 2000 entries that are added to the list.

Was it helpful?

Solution

Without seeing your actual code, there is no way to know for sure why your updates are that slow. However, if speed is an issue for you, especially with a lot of list items, you should put the TListView into virtual mode instead (set its OwnerData property to true) and store your status information elsewhere, not in the TListView itself (2000 items is a lot of overhead for a non-virtual ListView to handle). Then, simply call the ListView's Invalidate() or UpdateItems() method when needed to trigger repaints, and use the OnData event to provide the status data to TListView whenever it asks you for it. For example:

struct MyStatusInfo
{
    String Status;
    ...
};

MyStatusInfo StatusItems[2000];

__fastcall TForm1::TForm1(TComponent *Owner)
    : TForm(Owner)
{
    ...
    ListView1->Items->Count = 2000;  // you don't use Add() with a virtual ListView
    ...
}

void __fastcalll TForm1::UpdateStatus(int Index, const String &Status, ...)
{
    MyStatusInfo &Info = StatusItems[Item->Index];
    Info.Status = Status;
    ...
    ListView1->UpdateItems(Index, Index);
}

void __fastcall TForm1::ListView1Data(TObject *Sender, TListItem *Item)
{
    MyStatusInfo &Info = StatusItems[Item->Index];
    Item->Caption = Info.Status;
    ...
}

OTHER TIPS

I'm not sure if that helps, since BeginUpdate didn't, but it worth trying:

1) Try filling it while Enabled = false, then enable it

2) Try building a TListItems and assign it directly to the TListView's Items properties

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top