Back

TStringGrid
by James M Sandbrook of Tokoroa, New Zealand.


Loading text into one of the cells:

StringGrid1.Cells[c, r] := 'TStringGrid';
c = the number of cells across (The column number).
r = the number of cells down (The row number). 

Example:  StringGrid1.Cells[4,  4] := 'TStringGrid';
....

Loading text into one of the cells at the top of the (Fixed Row) TStringGrid:

StringGrid1.Cells[1, 0] := 'Name:';
This works the same as Loading Text into cells.
....

Adding two cells numbers together to get an answer:

procedure TForm1.Button1Click(Sender: TObject);
var A1, A2, A3: Integer;
 begin
     A1 := StrToInt(StringGrid1.Cells[1, 1]);
     A2 := StrToInt(StringGrid1.Cells[1, 2]);
     A3 := A1 + A2;
     StringGrid1.Cells[0, 3] := 'TOTAL:';
     StringGrid1.Cells[1, 3] := IntToStr(A3);
end;
Type in two numbers, put one in 1,1 and the other in 1,2. Then click on the button and the result will be in cell 1,3.
Make sure StringGrid1 - Options - goRowSelect is set to False and make sure StringGrid1 - Options - goEditing is set to True.
....

To change the Grid Lines width use:

procedure TForm1.Button2Click(Sender: TObject);
begin
    StringGrid1.GridLineWidth := 2;
end;
You can also do this in the Object Inspector.
....

Many things that work with TStringGrid, work with TDrawGrid.
....
To make the vertical line that separates each column from the next column invisible, set the Object Inspector property: Options - goVertLine - to False.
To make the horizontal line that separates each row from the next row invisible, set the Object Inspector property: Options - goHorzLine - to False.
....
To make the TStringGrid only allow the user to select one cell at a time during run-time,
set the Object Inspector property: Options - RangeSelect - to False.
....
To make the TStringGrid not allow users to resize the rows in run-time, set the
Object Inspector property: Options - goRowSizing - to False.
To make the TStringGrid not allow users to resize the columns in run-time, set
the Object Inspector property: Options - goColSizing - to False.
....
To make the TStringGrid not allow users to re-arrange the columns, set the Object
Inspector property: Options - goColMoving - to False.
To make the TStringGrid not allow users to re-arrange the rows, set the Object
Inspector property: Options - goRowMoving - to False.
....
To make the TStringGrid words editable during run-time, set the Object Inspector
property: Options - goEditing - to True.
....
To have the tabs set up for the TStringGrid, so that the user can tab through the
cells of the TStringGrid at run-time, set the Object Inspector property: Options - goTabs - to True.
....
If you want the user to be able to select a whole row at a time in the TStringGrid at run-time,
set the Object Inspector property: Options - goRowSelect - to True.
....
The amount of columns in your TStringGrid can be changed in the Object
Inspector property: ColCount.
....
The amount of rows in your TStringGrid can be changed in the Object
Inspector property: RowCount.
....
To change how thick the lines in your TStringGrid are, change the Object
Inspector property: GridLineWidth.
....
To change the amount of Fixed columns, change the Object Inspector property: FixedCols.

To change the amount of Fixed rows, change the Object Inspector property: FixedRows.
 ....

The Object Inspector property: 'FixedColor' changes the color of the thick heading blocks.

The Object Inspector property: 'Color' changes the color of the cells.
....

The Object Inspector property: 'DefaultColWidth' makes all of the columns in the TStringGrid
be as wide as the number you put in the Object Inspector property: 'DefaultColWidth'.
This can be changed for the different columns by placing the mouse cursor over the bar that
is at the end of the column you would like to make wider, and when the Split cursor comes
visible, drag the mouse to the size you would like the cell to be.
....
The Object Inspector property: 'DefaultRowHeight' makes all of the rows in the TStringGrid
be as high as the number you put in the Object Inspector property: 'DefaultRowHeight'.
This can be changed for the different rows by placing the mouse cursor over the bar that
is at the bottom of the row you would like to make higher, and when the Split cursor comes
visible, drag the mouse to the size you would like the row cells to be.
....
From Greg Liefs Delphi Question and Answer web page!

MULTI-LINE COLUMN HEADINGS IN A STRINGGRID:

Q. I'd like to display multi-line column headings in a TStringGrid. 

A. First, you need to set the grid's DefaultDrawing property to False. Next, you should use the
following logic as your OnDrawCell event handler for the grid. 

procedure TForm1.StringGrid1DrawCell(Sender: TObject; Col, Row: Longint;
  Rect: TRect; State: TGridDrawState);
var
   Line1 : string ;
   Line2 : string ;
   ptr : integer ;
   padding : integer ;
begin
   ptr := Pos(';', StringGrid1.Cells[Col, Row]) ;
   if ptr > 0 then begin
      Line1 := Copy(StringGrid1.Cells[Col, Row], 1, ptr - 1) ;
      Line2 := Copy(StringGrid1.Cells[Col, Row], ptr + 1, 
                    Length(StringGrid1.Cells[Col,Row]) - ptr) ;
   end
   else
      Line1 := StringGrid1.Cells[Col, Row] ;
   StringGrid1.Canvas.FillRect(Rect) ;
   StringGrid1.Canvas.TextOut(Rect.Left, Rect.Top + 2, Line1) ;
   if ptr > 0 then
      StringGrid1.Canvas.TextOut(Rect.Left, Rect.Top -
                  StringGrid1.Canvas.Font.Height + 3, Line2) ;
end ;

Now you can embed a semi-colon to denote a line break in the column heading. Also remember
to make row zero deep enough to accommodate the multiple lines, for example, by including the
following statement in your OnCreate event handler for the form: 

StringGrid1.RowHeights[0] := StringGrid1.DefaultRowHeight * 2 ;

Back