gpt4 book ai didi

delphi - TStringGrid 性能不佳

转载 作者:行者123 更新时间:2023-12-03 14:55:03 26 4
gpt4 key购买 nike

我有一个包含 10 列的 TStringGrid。添加 500 行大约需要 2 秒。这是正常表现吗?

我觉得有点慢。

我正在从数据库查询中获取数据。如果我循环查询但不将结果写入 StringGrid,则该过程大约需要 100 毫秒,因此并不是数据库导致速度变慢。

添加行后,StringGrid 性能就很好。

这是我正在使用的代码

Grid.RowCount := Query.RecordCount;
J := 0;

while not Query.EOF do
begin
Grid.Cells[0,J]:=Query.FieldByName('Value1').AsString;
Grid.Cells[1,J]:=Query.FieldByName('Value2').AsString;
Grid.Cells[2,J]:=Query.FieldByName('Value3').AsString;
// etc for other columns.
Inc(J);
Query.Next();
end;

真正的代码实际上有点复杂(表列与查询列并不完全对应),但这就是基本思想

最佳答案

我发现在浏览大量记录时非常重要的另一件事是为每个字段使用正确的 TField 变量。 FieldByName 每次都会迭代 Fields 集合,因此不是性能最佳的选项。在循环之前定义每个字段,如下所示:

var
f1, f2: TStringField;
f3: TIntegerField;

begin
// MyStringGrid.BeginUpdate; // Can't do this
// Could try something like this instead:
// MyStringGrid.Perform(WM_SETREDRAW, 0, 0);
try
while ... do
begin
rowvalues[0] := f1.AsString;
rowvalues[1] := f2.AsString;
rowvalues[2] := Format('%4.2d', f3.AsInteger);
// etc
end;
finally
// MyStringGrid.EndUpdate; // Can't - see above
// MyStringGrid.Perform(WM_SETREDRAW, 1, 0);
// MyStringGrid.Invalidate;
end;
end;

与 BeginUpdate/Endupdate 一起使用,并在适当的情况下调用 Query.DisableControls。

关于delphi - TStringGrid 性能不佳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7669621/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com