In Delphi, is there a fast way of emptying a TStringgrid (containing in excess of 5000 rows) that will also free the memory?
Setting the rowcount to 1, empties the grid but does not free the memory.
Thanks in advance,
Paul
This should uninitialize the allocated strings (from the string list where the row texts are stored). Cleaning is done by columns since you have a lot of rows.
procedure TForm1.Button1Click(Sender: TObject);
var
I: Integer;
begin
for I := 0 to StringGrid1.ColCount - 1 do
StringGrid1.Cols[I].Clear;
StringGrid1.RowCount := 1;
end;
By "does not free the memory", do you mean that if you set RowCount := 1
, and then set the RowCount := 10' you can still see the old content of the
Cells`?
If so, this is an old issue and has nothing to do with the memory not being freed; it's simply because you just happen to see the previous content of the memory when it's allocated again, because memory isn't zero'd out.
I have a pretty standard routine in a utility unit that deals with this visual glitch, and unless the grid is huge works fast enough. Just pass the TStringGrid
before you change the RowCount
or ColCount
to a lower value.
procedure ClearStringGrid(const Grid: TStringGrid);
var
c, r: Integer;
begin
for c := 0 to Pred(Grid.ColCount) do
for r := 0 to Pred(Grid.RowCount) do
Grid.Cells[c, r] := '';
end;
Use it like this:
ClearStringGrid(StringGrid1);
StringGrid1.RowCount := 1;
我建议将您的字符串值存储在您可以完全控制的自己的内存中,然后使用 TDrawGrid 或更好的虚拟 TListView 来根据需要显示该内存的内容。
The fastest way to use a TStringGrid is using OnGetValue/OnSetValue. This way only the text of visible cells are requested dynamically. Adding and removing rows is then lighting fast, otherwise TStringgrid is very slooow when you have more than 5000 records. This way I can fill and clear a grid with 700.000 records within a second!
When memory usage is the critical argument, consider using another grid. For example, NLDStringGrid that is (re)written by myself, and which has an additional property called MemoryOptions
. It controls whether data can be stored beyond ColCount * RowCount
, whether the storage is proportional (less memory usage for partially filled rows and columns), whether to store the Cols
and Rows
property results and whether the data is stored in sparse manner.
To clear such grid that has moBeyondGrid
excluded from the memory options, setting RowCount
to FixedRows
suffices.
It's open source and downloadable from here .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.