uses
LCLIntf, LCLType;
// Measures the height of the rectangle of a word-wrapped cell
function CalcCellHeight(AGrid: TStringGrid; ACol, ARow: Integer): Integer;
var
R: TRect;
s: String;
begin
R := AGrid.CellRect(ACol, ARow);
R.Bottom := R.Top;
s := AGrid.Cells[ACol, ARow];
DrawText(AGrid.Canvas.Handle, PChar(s), Length(s), R, DT_CALCRECT or DT_WORDBREAK or DT_NOPREFIX);
Result := R.Bottom - R.Top;
end;
// This calculates the maximum required cell height of a specific row
function CalcRowHeight(AGrid: TStringGrid; ARow: Integer): Integer;
var
col: Integer;
h, hmax: Integer;
begin
hmax := AGrid.DefaultRowHeight;
for col := 0 to AGrid.ColCount-1 do
begin
h := CalcCellHeight(AGrid, col, ARow);
if h > hmax then hmax := h;
end;
Result := hmax;
end;
// This is the final procedure. It calls CalcRowHeight for all rows of the grid.
procedure AutoRowHeights(AGrid: TStringGrid);
var
row: Integer;
begin
for row := 0 to AGrid.RowCount-1 do
AGrid.RowHeights[row] := CalcRowHeight(AGrid, row) + 2*varCellPadding;
end;