1

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?

1 Answers1

3

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;
jachguate
  • 16,976
  • 3
  • 57
  • 98
  • Thanks for the solution. It really works. I was using string grid for the first time. So, I thought there might be some property which fills it in reverse order. –  Nov 28 '12 at 05:45
  • 1
    @Naresh - you can use in-memory dataset and TDBGrid. Then you'd be able to insert row in TDataSet terms. If the grid has well-defined and strict format, that might be the most useful approach. – Arioch 'The Nov 28 '12 at 09:34
  • 1
    @Arioch the question is about TStringGrid, no need for a TDBGrid and a DataSet in this case. – jachguate Nov 28 '12 at 09:36
  • Basing at the level of the questions, i don't think he even new that si possible, So let he has one mroe tool at his disposal. Whether it suits his situations or not, we can not know for sure. But you better know of alternateive approaches to avoid "it all look nails" situation. – Arioch 'The Nov 28 '12 at 09:51
  • @Arioch'The My requirement is simple. I have to fill grid with the value of textbox whenever anything is entered in textbox. So I think StringGrid will do my job. Taking dataset, then datasource and then dbgrid will make it complex. But, thanks for giving alternate solution. –  Nov 28 '12 at 11:48