How to quickly add many rows to a TStringGrid?

Mawg says reinstate Monica picture Mawg says reinstate Monica · Oct 12, 2012 · Viewed 7.9k times · Source

I googled and found a lot of advice, but it all seemed several years old and none of it helped.

I have a string grid with 8 columns and once I get more than a few hundred rows it is taking over 2 seconds to populate (I compared using GetTickCount).

I tried StringGrid.Perform(WM_SETREDRAW, 0, 0) (and 0, 1 at the end). I tried setting Visible := False while updating. Both to no use.

There is no BeginUpdate() method.

Any advice? Delphi XE2 starter. I would be willing to use a FOSS 3rd party VCL string grid if it is tried & tested.


[Update] using TDrawGrid ... "A TDrawGrid doesn't have a property "Cells", like its brother TStringGrid. Your code has to calculate where to display the data and next it must draw a representation of the data on the "Canvas" of the grid."

That sounds like a lot of work to me :-(

Using VirtualTreeView - sounds ok if it is fast enough. I just won't have any child nodes. (update++ I just read this on the homepage "Virtual Treeview is extremely fast. Adding one million nodes takes only 700 milliseconds"). No problems on speed, then. But it woudl nice to just use a string grid. Escpecially one where the user can click & sort.

Alternatively, the stringgrid is only 20 rows high. Maybe I could just handle scrolbar clicks and clear & repopulate those 20 rows when the user scrols?

[Furtehr update] I changed from TStringGrid to TListView which codes have Beginupdate()), but that made a negligible difference. Ops, I forgot "viortual mode" - brb.

Btw, the data are read-only, just for disply.

Surely this is a very common probem?

Answer

NGLN picture NGLN · Oct 12, 2012

Add rows from bottom to top, and/or set RowCount afterwards. I just did a little test adding 90.000 rows, gaining ca. 25% in speed.

Though, this takes 1,5 seconds at most. Since you are talking about just a few hundred rows, I am sure populating the grid is not the burden here. Instead the time needed to retrieve and/or convert the data seems to be.