Forum > LCL

TStringGrid with stretched rows heights according to content of cells

<< < (2/3) > >>

jamie:
You can use the OnPrepairCanvas event of the StringGrid to calculate the required row height.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.StringGrid1PrepareCanvas(sender: TObject; aCol, aRow: Integer;  aState: TGridDrawState);begin  if (aRow = 2)and(aCol =0) then TStringGrid(Sender).RowHeights[2]:=30;end;                
 here I simply set a blank grid's row2 to 30 pixels high.

 you should calculate the required height once so that all columns will share the same value.

 I assume you make want to box in some text ?

PasCoder:
Dear Jamie,
Thank you for your response. However, your solution looks to be more MANUAL than AUTOMATIC. I would like the entire StringGrid to automaitcally adjust each row's size depending on the data/text in various cells. So, I thought that the Class (TStringGridHelper) that Lacak posted does that. Can you look at it and point me in the right direction of using it?

Thank you

wp:
Forget the stringgrid helper - it's just a formal thing. Try to understand the algorithm to use for your request: You must iterate over all cells in a row, and for each cell you measure the height of the cell rectangle with the word-wrapped text. This can be done by calling the LCLIntf function DrawText with the flags DT_CALCRECT and DT_WORDBREAK (read the Windows API for this function: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-drawtext). Then determine the maximum of all cell heights in this row and assign this to the RowHeight of that particular grid row. Then repeat with the next row, until all rows have been handled.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses  LCLIntf, LCLType; // Measures the height of the rectangle of a word-wrapped cellfunction 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 rowfunction 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;
This is essentially the same as Lacak's code, just written as simple procedures.

A class helper provides a mechanism to add new methods to a class without changing its source code. Simply invoke the methods of the class helper as if they were methods of the class. Lacak implements a method AutoSizeRow(ARow: Integer). So, when your stringgrid is named StringGrid1 call StringGrid1.AutoSizeRow(ARow) - essentially the same as my code which puts the grid into the parameter list.

PasCoder:
Dear wp;
Thanks for the quick help. It seems it will help me a lot but suppose I port my Project to another OS like Linux or Mac, will it still work?

Thank you

wp:
Yes, definitely. Because of "uses LCLIntf, LCLType" instead of "uses Windows". These units provide most of what is contained in the Windows unit in a cross-platform way.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version