Delay in Repopulating TTMSFMXGrid

I have a function that populates a TTMSFMXGrid from a database query. The first time I call the function, the grid populates quickly (under 100 milliseconds). The second time I call the same function with the same parameters it takes about 13000+ milliseconds to populate the grid. Am I doing something wrong in the function below that would cause the delay in the second execution?

procedure TfrmMain.LoadRegistrations(ProductID: integer);
var
i,
r : integer;
StartTime,
StopTime: TDateTime;
begin
grdRegister.RowCount := 2;
grdRegister.RowSelect[1] := False;
for i := 0 to grdRegister.Columns.Count-1 do
grdRegister.Cells[i,1] := '';
r := 1;
with SQLData do begin
with qryBlank do begin
Active := False;
SQL.Clear;
SQL.Add ('select RegID, C.Company_Name, P.Prod_Name,'
+ ' Ordered = V1.Version, Order_Rcvd_Date,'
+ ' Sent = V2.Version, Reg_Date'
+ ' from Registration R'
+ ' INNER JOIN Customer C'
+ ' ON R.CustID = C.CustID'
+ ' INNER JOIN Product P'
+ ' ON R.ProdID = P.ProdID'
+ ' LEFT JOIN Version V1'
+ ' ON R.Ordered_VersionID = V1.VersionID'
+ ' LEFT JOIN Version V2'
+ ' ON R.Sent_VersionID = V2.VersionID');
if chkVoid.IsChecked then
SQL.Add (' WHERE R.Pymnt_method = ' + QuotedStr('V'))
else
SQL.Add (' WHERE R.Pymnt_method <> ' + QuotedStr('V'));
if ProductID > 0 then
SQL.Add (' and R.ProdID = ' + IntToStr(ProductID));
SQL.Add (' order by R.Order_Rcvd_Date desc, RegID desc');
Active := True;
grdRegister.RowCount := RecordCount;
First;
end;
StartTime := GetTime;
with qryBlank do
while not EOF do begin
grdRegister.Cells[0,r] := FieldByName('Company_Name').AsString;
grdRegister.Cells[1,r] := FieldByName('Prod_Name').AsString;
grdRegister.Cells[2,r] := FieldByName('Ordered').AsString;
grdRegister.Cells[3,r] := FormatDateTime('mm/dd/yyyy',
FieldByName('Order_Rcvd_Date').AsDateTime);
if not FieldByName('Reg_Date').IsNull then begin
grdRegister.Cells[4,r] := FieldByName('Sent').AsString;
grdRegister.Cells[5,r] := FormatDateTime('mm/dd/yy',
FieldByName('Reg_Date').AsDateTime);
grdRegister.Cells[6,r] := 'X'
end
else begin
grdRegister.Cells[4,r] := '';
grdRegister.Cells[5,r] := '';
grdregister.Cells[6,r] := '';
end;
grdRegister.Cells[7,r] :=
IntToStr(qryBlank.FieldByName('RegID').AsInteger);
Next;
inc(r);
end;
StopTime := GetTime;
qryBlank.Active := False;
ShowMessage(IntToStr(MillisecondsBetween(StopTime, StartTime)));
end;
end;

Please use

grdRegister.BeginUpdate;

// CODE

grdRegister.EndUpdate;

Thanks. That did the trick. Can you tell me why that made a difference? (Just trying to understand how the grid works.)

Because each property that visually updates the grid will do some re-calculation. This takes time. When Beginupdate & EndUpdate is used, the calculation are bundled in 1 call.