Board index » delphi » Optimized columns size in stringgrids

Optimized columns size in stringgrids

Hi!

I search a possibility to automatically adjust the size of a column to the
text with the biggest width in the whole column.
Any ideas?

CU, Sebastian

 

Re:Optimized columns size in stringgrids


In article <88rnfs$s3...@news.cityweb.de>, "Sebastian Tengler"

Quote
<sebast...@tengler-online.de> writes:
>I search a possibility to automatically adjust the size of a column to the
>text with the biggest width in the whole column.
>Any ideas?

I'm afraid you have to roll-your-own <g> like this :-

procedure SetColWidths(Grid : TStringGrid);
// sub-proc start =========================================
  function Max(A, B : integer) : integer;
  begin
    Result := A;
    if B > A then
      Result := B;
  end;
// sub-proc end ===========================================
var
  i, j, ColMax : integer;
begin
  with Grid do
    for i := 0 to ColCount - 1 do
      if i in [1,3, 4] then begin  // or whatever columns you want sized
        ColMax := 0;
        for j := 0 to RowCount - 1 do
          ColMax := Max(ColMax, Canvas.TextWidth(Cells[i, j]));
        ColWidths[i] := ColMax + 4; // 4 for a margin
      end; {if i in [1,3 4]}
    {end; for i := 0 to ColCount - 1}
  {end; with Grid}
end;

Of course if you've got the Delphi version with Math.pas you can put that in
your uses clause and use . . .

 ColMax := MaxIntValue([ColMax, Canvas.TextWidth(Cells[i, j])]);

 . . . instead of the sub-function.

Alan Lloyd
alangll...@aol.com

Other Threads