Re: Adding rows to a TStringGrid

From: AlanGLLoyd (alanglloyd_at_aol.com)
Date: 10/14/04


Date: 14 Oct 2004 08:39:25 GMT

There is a protected method of TCustomGrid which is not exposed in TStringgrid.
You may expose it by declaring a descendant class and typecasting the
stringgrid to that class, Then you can increase the SG.RowCount and move the
last row to where you want it. Use LockWindowUpdate to improve speed of insert
and redrawing ...

type
  TMoveRowSG = class(TStringGrid); // declare descendant

procedure TForm1.InsertSGRow(SG : TStringGrid; RowOrigin : integer;
                             InsertBefore : boolean);
begin
  LockWindowUpdate(SG.Handle); // prevent incremental drawing of SG
  {add row at end}
  SG.RowCount := SG.RowCount + 1;
  {move row to defined position}
  if InsertBefore then
    TMoveRowSG(SG).MoveRow(SG.RowCount - 1, RowOrigin)
  else
    if RowOrigin < SG.RowCount - 2 then // don't move row to same place
      TMoveRowSG(SG).MoveRow(SG.RowCount - 1, RowOrigin + 1);
  LockWindowUpdate(0); // redraw all SG now
end;

Alan Lloyd
alanglloyd@aol.com