Jump to content


dee

Member Since 05 Dec 2006
Offline Last Active Dec 17 2007 01:06 PM
-----

Topics I've Started

Suggestion to improve speed

03 December 2007 - 09:29 AM

Hi all, I'm looking for suggestions to improve the speed for populating the NextGrid with data from a dataset. For this post, I've written a simple program that displays the result of a given query.

CODE

procedure TMainForm.btnExecuteClick(Sender: TObject);
var
TempStart, TempEnd: TTime;
TempSQL: string;
TempDataSet: TDataSet;
i, j: Word;
begin
Grid.BeginUpdate;
Grid.ClearRows;
Grid.Columns.Clear;
try
TempSQL := memoSQLCommand.Text;
TempDataSet := ExecuteQuery(TempSQL, False);

if TempDataSet.FieldCount > 0 then
begin
for i := 0 to TempDataSet.FieldCount - 1 do
AddGridCol(Grid, False, True , Grid.Columns.Count, TNxTextColumn, TempDataSet.Fields[i].FieldName, 80, True, taLeftJustify);
end;

TempStart := Now;
if TempDataSet.RecordCount > 0 then
begin
Grid.AddRow();
Grid.AddRow(TempDataSet.RecordCount - 1);
j := 0;

while not TempDataSet.Eof do
begin
for i := 0 to TempDataSet.FieldCount - 1 do
Grid.Cells[i, j] := TempDataSet.Fields[i].AsString;

TempDataSet.Next;
Inc(j);
end;
end;
TempEnd := Now;
finally
TempDataSet := nil;
FreeAndNil(TempDataSet);
end;
Grid.EndUpdate;
ShowMessage(FormatDateTime('s.zzzz', TempEnd - TempStart));
end;

AddGridCol is a simple procedure to add a column to the NextGrid with some properties at runtime.

CODE

Grid.AddRow();
Grid.AddRow(TempDataSet.RecordCount - 1);

Notice the part above. If I do not do this, there will be an "out of bounds" error.

With this code, I could populate the grid with 29 and 8,000 records at around 4.250 seconds. I think it's a bit slow, any suggestions to improve speed will be much appreciated. Thanx in advance smile.gif