| 
			Author: Tomas Rutkauskas
How to display multiple lines in a single cell of a TStringGrid
Answer:
Solve 1:
It is fairly easy to draw such text in an OnDrawCell handler. However the standard 
inplace editor of a grid is not well prepared to handle cells higher than one line.
1   
2   procedure TForm1.StringGrid1DrawCell(Sender: TObject; Col, Row: Integer;
3     Rect: TRect; State: TGridDrawState);
4   var
5     S: string;
6     drawrect: TRect;
7   begin
8     S := (Sender as TStringGrid).Cells[Col, Row];
9     if Length(S) > 0 then
10    begin
11      drawrect := rect;
12      DrawText((Sender as TStringGrid).canvas.handle, Pchar(S), Length(S), drawrect,
13        DT_CALCRECT or DT_WORDBREAK or DT_LEFT);
14      if (drawrect.bottom - drawrect.top) > (Sender as TStringGrid).RowHeights[row] 
15  then
16        (Sender as TStringGrid).RowHeights[row] := (drawrect.bottom - drawrect.top)
17      else
18      begin
19        drawrect.Right := rect.right;
20        (Sender as TStringGrid).canvas.fillrect(drawrect);
21        DrawText((Sender as TStringGrid).canvas.handle, Pchar(S), Length(S),
22          drawrect, DT_WORDBREAK or DT_LEFT);
23      end;
24    end;
25  end;
It will automatically adjust the row height to larger values if needed. The main 
problem is that it will not automatically decrease the row height if the text would 
fit into a smaller row. It cannot do that since there may be other cells in the row 
that need a taller row. You could fix that problem by setting the rowheigh back to 
the defaultrowheight when you change the cell data for a row, that would trigger a 
redraw and that in turn would adjust the rowheight to what is needed. It would 
cause some flicker, though.
Solve 2:
This is a unit that enables word-wrap in a cell of a TStringGrid:
26  unit Unit1;
27  
28  interface
29  
30  uses
31    SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
32    Forms, Dialogs, ExtCtrls, Grids;
33  
34  type
35    TForm1 = class(TForm)
36      StringGrid1: TStringGrid;
37      Panel1: TPanel;
38      procedure StringGrid1DrawCell(Sender: TObject; Col, Row: Longint;
39        Rect: TRect; State: TGridDrawState);
40    private
41      { Private declarations }
42    public
43      { Public declarations }
44    end;
45  
46  var
47    Form1: TForm1;
48  
49  implementation
50  
51  {$R *.DFM}
52  
53  procedure TForm1.StringGrid1DrawCell(Sender: TObject; Col, Row: Longint;
54    Rect: TRect; State: TGridDrawState);
55  var
56    A: array[0..255] of char;
57    drawrect: TRect;
58  begin
59    StrPCopy(A, (Sender as TStringgrid).Cells[Col, Row]);
60    if StrLen(A) > 0 then
61      with (Sender as TStringgrid) do
62      begin
63        drawrect := rect;
64        {Note that the first DrawText uses the DT_CALCRECT flag to calculate a new
65  			value for drawrect. This value is used to set the RowHeights 
66  			parameter if necessary.}
67        DrawText(canvas.handle, A, -1, drawrect, DT_CALCRECT or DT_WORDBREAK or
68          DT_LEFT);
69        if (drawrect.bottom - drawrect.top) > RowHeights[Row] then
70          RowHeights[row] := (drawrect.bottom - drawrect.top)
71        else
72        begin
73          drawrect.right := rect.right;
74          canvas.fillrect(drawrect);
75          DrawText(canvas.handle, A, -1, drawrect, DT_WORDBREAK or DT_LEFT);
76        end;
77      end;
78  end;
79  
80  end.
			 |