Delphi – Filling a TStringGrid in Delphi

delphidelphi-xe2tstringgrid

I have a TStringGrid say StringGrid1 and one textbox say textbox1 on my delphi form. When I enter anything in textbox1, that comes in next row of the StringGrid1.

I want that the new enteries in StringGrid1 should come on top rather than on bottom. Which property should I change?

Best Answer

AFAIK There's no property you can tweak to insert a row in some position of a StringGrid.

But, you can always do a shift in code to make space in the grid for a new row.

Assuming you have a first row and columns of titles, you may code it as:

procedure TForm1.Button1Click(Sender: TObject);
var
  I: Integer;
begin
  StringGrid1.RowCount := StringGrid1.RowCount + 1;
  for I := StringGrid1.RowCount - 1 downto 1 do
    StringGrid1.Rows[I] := StringGrid1.Rows[I - 1];
  StringGrid1.Cols[1][1] := Edit1.Text;
  //the commented line comes from my quick test.
  //Edit1.Text := IntToStr(StringGrid1.RowCount);
end;