Forum > LCL

TStringGrid with stretched rows heights according to content of cells

<< < (3/3)

PasCoder:
Dear wp,
Thank you very much. I appreciate your support.

Vodnik:
I gratefully used this code in my application. It works fine with normal grid. When I enabled columns, I noticed some issues:

1. The title row is no more autosized.

2. Following string causes arithmetic overflow error in case of hidden column:

--- 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";}};} ---RowHeights[aRow] := maxRowHeight+2;
For hidden column (e.g. StringGrid1.Columns[2].Visible:=False;) column width is 0 (ColWidths[2]=0).
In that case maxRowHeight stays equal to MaxInt.
I've add check for ColWidths[aCol]>0 as a workaround, while not sure this will work for all systems.

(Windows 10, Lazarus 2.2.4)

wp:

--- Quote from: Vodnik on December 05, 2023, 11:02:21 am ---1. The title row is no more autosized.

--- End quote ---
Yes, the title row requires extra handling when Columns are enabled:

--- 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";}};} ---function CalcCellHeight(AGrid: TStringGrid; ACol, ARow: Integer): Integer;var  R: TRect;  s: String;begin  R := AGrid.CellRect(ACol, ARow);  R.Bottom := R.Top;  if (ARow = 0) and (ACol < AGrid.Columns.Count) then  begin    s := AGrid.Columns[ACol].Title.Caption;    AGrid.Canvas.Font.Assign(AGrid.columns[ACol].Title.Font);  end else  begin    s := AGrid.Cells[ACol, ARow];    AGrid.Canvas.Font.Assign(AGrid.Font);  end;  DrawText(AGrid.Canvas.Handle, PChar(s), Length(s), R, DT_CALCRECT or DT_WORDBREAK or DT_NOPREFIX);  Result := R.Bottom - R.Top;end;

--- Quote from: Vodnik on December 05, 2023, 11:02:21 am ---2. Following string causes arithmetic overflow error in case of hidden column:

--- 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";}};} ---RowHeights[aRow] := maxRowHeight+2;
--- End quote ---
Please post a demo project on this one.

Vodnik:
WP, your code works fine for StringGrids with and without Columns.
Second issue I've noticed in LacaK's code, which is publiced in WiKi, so I have discovered it first.

wp:

--- Quote from: Vodnik on December 06, 2023, 04:22:51 pm ---Second issue I've noticed in LacaK's code, which is publiced in WiKi, so I have discovered it first.

--- End quote ---
Tried to construct a full project around LacaK's code above, but could not reproduce any failure. Still: Please submit a project that can be compiled.

Navigation

[0] Message Index

[*] Previous page

Go to full version