TalkingCode
TalkingCode

Reputation: 13567

How to (fast) fill a CListCtrl in C++ (MFC)?

in my application I have a few CListCtrl tables. I fill/refresh them with data from an array with a for-loop. Inside the loop I have to make some adjustments on how I display the values so data binding in any way is not possible at all.

The real problem is the time it takes to fill the table since it is redrawn row by row. If I turn the control invisible while it is filled and make it visible again when the loop is done the whole method is a lot faster!

Now I am looking for a way to stop the control from repainting until it is completely filled. Or any other way to speed things up.

Upvotes: 8

Views: 6753

Answers (2)

Kirill V. Lyadvinsky
Kirill V. Lyadvinsky

Reputation: 99665

If you have a lot of records may be it is more appropriate to use virtual list style (LVS_OWNERDATA). You could find more information here.

Upvotes: 7

Nick Meyer
Nick Meyer

Reputation: 40372

Look into the method SetRedraw. Call SetRedraw(FALSE) before starting to fill the control, SetRedraw(TRUE) when finished.

I would also recommend using RAII for this:

class CFreezeRedraw
{
public:
   CFreezeRedraw(CWnd & wnd) : m_Wnd(wnd) { m_Wnd.SetRedraw(FALSE); }
   ~CFreezeRedraw() { m_Wnd.SetRedraw(TRUE); }
private:
   CWnd & m_Wnd;
};

Then use like:

CFreezeRedraw freezeRedraw(myListCtrl);
//... populate control ...

You can create an artificial block around the code where you populate the list control if you want freezeRedraw to go out of scope before the end of the function.

Upvotes: 20

Related Questions