Auto adjust row heights in TStringGrids (AutoSizeRow)
Original Reporter info from Mantis: lacak @lacak.sk
-
Reporter name: LacaK
Original Reporter info from Mantis: lacak @lacak.sk
- Reporter name: LacaK
Description:
There already exists public method TCustomStringGrid.AutoSizeColumn
(which calls protected TCustomGrid.AutoAdjustColumn which calls TCustomStringGrid.AutoAdjustColumn)
whichs determines needed column width based on current cell content and also takes into account Title.Multiline.
My idea/proposal is that it would be useful also have something similar for RowHeights.
So rows are stretched/enlarged based on individual cell contents in a given row.
I implemented it as helper method (see steps to reproduce).
It is simple so code is self explaining.
Please consider if it match design of TCustomGrid / TCustomStringGrid ... and if yes integrate method into TCustomGrid/TCustomStringGrid
Steps to reproduce:
{ TStringGridHelper }
TStringGridHelper = class helper for TStringGrid
protected
procedure AutoSizeRow(aRow: integer);
end;
{ TStringGridHelper }
procedure TStringGridHelper.AutoSizeRow(aRow: integer);
var
aCanvas: TCanvas;
aCol, maxRowHeight:integer;
aText: String;
textRect: TRect;
begin
aCanvas := GetWorkingCanvas(Canvas);
maxRowHeight := DefaultRowHeight;
for aCol:=0 to ColCount-1 do begin
aText := Cells[aCol,aRow];
textRect := Rect(0, 0, ColWidths[aCol], MaxInt);
DrawText(aCanvas.Handle, PChar(aText), Length(aText), textRect, DT_CALCRECT or DT_WORDBREAK);
if maxRowHeight < textRect.Height then
maxRowHeight := textRect.Height
end;
if aCanvas<>Canvas then FreeWorkingCanvas(aCanvas);
RowHeights[aRow] := maxRowHeight+2;
end;
Mantis conversion info:
- Mantis ID: 38473