CListView Population is slow (populate with std::string and doubles)

Posted on 2009-04-23
Last Modified: 2013-11-20
Dear Experts

I am creating an MFC application where one of my document views is a list view. The data I want to populate the list with is a time series, the header row being the name of the series, the first column being times, and the rest of the columns should be filled with data. In some sense this resembles the view in Excel.

My problem is that populating the listview is extremely slow. The code doing that part is:

void CSeriesView::InsertData()
      LVITEM lvItem;
      int nItem;
      CListCtrl& m_List = GetListCtrl();

      CSeriesDoc *iDoc = GetDocument();
      unsigned int nr = iDoc->iSeries.timelength();
      unsigned int nc = iDoc->iSeries.serieslength();
      for (unsigned int i=0; i<nr; i++)
            lvItem.mask = LVIF_TEXT;
            lvItem.iItem = i;
            lvItem.iSubItem = 0;
            std::stringstream iS;
            iS << iDoc->iSeries.t(i);
            lvItem.pszText = str2lpstr(iS.str());
            nItem = m_List.InsertItem(&lvItem);

            for (unsigned int j=0; j<nc; j++)
                  double d_ = iDoc->iSeries(i,j);
                  std::string s_ = boost::lexical_cast<std::string,double>(d_);
                  m_List.SetItemText(nItem, j+1, str2lpstr(s_));

The iSeries is my time series object, its member function t simply returns the time (boost posix_time) for the current row, str2lpstr is my own function converting a std::string to a lpstr and the boost::lexical_cast is my own clumsy way of converting a double to a std::string.

I guess all this stupid conversion is what slows down my application. But it could of course also be the way I use ListView in general. I hope some of you have an idea or two that would help me out.

Thanks, Peter
Question by:PeterTokeHedenAhlgren
    LVL 30

    Accepted Solution

    Hi PeterTokeHedenAhlgren,

    how many rows do you insert into the list? Is it slow even if you don't add really much items? If you insert much more items than visible a significant speed increasement can be reached using the list view as a virtual list control. A description including sample code for this can be found here:


    Author Comment

    Ii intend to add >10000 rows. And you are right. Whenever I use a low number of items, speed is all right.

    Thanks a lot Zoppo. You are a true "wizard"
    LVL 30

    Expert Comment


    Thanks for the compliment ...

    Nice if I could help you.

    Have a nice day,

    best regards,


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
    Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    This video discusses moving either the default database or any database to a new volume.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now