Search code examples
delphitstringgrid

How to quickly add many rows to a TStringGrid?


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?


Solution

  • Adding to others suggesting a virtual string grid I'd like to plug TdzVirtualStringGrid which builds on a TDrawGrid and adds events to return the strings to display. I am using it for very "large" grids and it works fine.

    Download it here.

    (You need more files from the same repository, this is just the main component source code.)

    EDIT: By "more files from the same repository" I mean, that this component uses other units from the dzlib library, so you should probably check out the whole shebang and either add it to your program's search path (there is a lot more useful stuff in there, because I add to it whenever I come across something that needs a more generalized solution) or just extract those units which the component depends on. dzlib is licensed under MPL.