How do I make a StringGrid's columns fit the grid's width?
Asked Answered
M

5

8

I've been looking for a long time for a solution without any luck. Does anyone know a simple way to do that? I would like to stretch for example the second colum of my grid to fit the grid's width!

Meridithmeriel answered 25/10, 2011 at 3:4 Comment(0)
S
10

Use the ColWidths property, like so:

with StringGrid1 do
  ColWidths[1] := ClientWidth - ColWidths[0] - 2 * GridLineWidth;

And for a more robust and flexible solution, take all fixed columns into account and parameterize the column index:

procedure SetColumnFullWidth(Grid: TStringGrid; ACol: Integer);
var
  I: Integer;
  FixedWidth: Integer;
begin
  with Grid do
    if ACol >= FixedCols then
    begin
      FixedWidth := 0;
      for I := 0 to FixedCols - 1 do
        Inc(FixedWidth, ColWidths[I] + GridLineWidth);
      ColWidths[ACol] := ClientWidth - FixedWidth - GridLineWidth;
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  SetColumnFullWidth(StringGrid1, 4);
end;
Subastral answered 25/10, 2011 at 3:14 Comment(2)
Thank you!! this is how it works for me: code procedure SetColumnFullWidth(Grid: TStringGrid; ACol: Integer); var I: Integer; FixedWidth: Integer; begin FixedWidth := 0; for I := 0 to Grid.ColCount - 1 do begin if(I=0) then begin Grid.ColWidths[I] := 50; FixedWidth := FixedWidth +50; end else begin Grid.ColWidths[I] := 100; FixedWidth := FixedWidth +100; end; end; Grid.ColWidths[ACol] := (Grid.Width-FixedWidth)+90; end; codeMeridithmeriel
This solution is vastly different from what a normal use case would be, but does in fact answer the original question to set exactly one column to the whole StringGrid width. The other solutions misunderstood the (granted, unintuitive) question and provided other solutions to "auto size" column widths of all columns. They all have their uses for some use-cases.Holytide
C
2

The following code works with FixedCols = 0 (to adapt for other values, ex: FixedCols = 1 ==> for Col := 1 to ...)

procedure AutoSizeGridColumns(Grid: TStringGrid);
const
  MIN_COL_WIDTH = 15;
var
  Col : Integer;
  ColWidth, CellWidth: Integer;
  Row: Integer;
begin
  Grid.Canvas.Font.Assign(Grid.Font);
  for Col := 0 to Grid.ColCount -1 do
  begin
    ColWidth := Grid.Canvas.TextWidth(Grid.Cells[Col, 0]);
    for Row := 0 to Grid.RowCount - 1 do 
    begin
      CellWidth := Grid.Canvas.TextWidth(Grid.Cells[Col, Row]);
      if CellWidth > ColWidth then
        Grid.ColWidths[Col] := CellWidth + MIN_COL_WIDTH
      else
        Grid.ColWidths[Col] := ColWidth + MIN_COL_WIDTH;
    end;
  end;
end;
Checker answered 8/11, 2018 at 21:42 Comment(0)
C
2

even better like this :

procedure AutoSizeGridColumns(Grid: TStringGrid);
const
  MIN_COL_WIDTH = 15;
var
  Col : Integer;
  ColWidth, CellWidth: Integer;
  Row: Integer;
begin
  Grid.Canvas.Font.Assign(Grid.Font);
  for Col := 0 to Grid.ColCount -1 do
  begin
    ColWidth := Grid.Canvas.TextWidth(Grid.Cells[Col, 0]);
    for Row := 0 to Grid.RowCount - 1 do 
    begin
      CellWidth := Grid.Canvas.TextWidth(Grid.Cells[Col, Row]);
      if CellWidth > ColWidth then
        ColWidth := CellWidth
    end;
    Grid.ColWidths[Col] := ColWidth + MIN_COL_WIDTH;
  end;
end;
Checker answered 9/11, 2018 at 7:38 Comment(1)
I literally just copy&pasted this code into my form unit and it does exactly what I hoped, thank you!Holytide
E
1

Allows you to dynamically resize columns depending on the size of the container, given the dimensions of the content.

Call AutoSizeGridColumns when resizing occurs and pass it a TStringGrid object

    procedure AutoSizeGridColumns(Grid: TStringGrid);
    var
      ACol, ARow: Integer;
      GridWidth, ColWidth, ColsCount: Integer;
      ColWidthDifferenceWidth, ColMinWidth, ColsSumWidth: Integer;
    begin
      GridWidth := Grid.Width;
      ColsCount := Grid.ColCount;      
      ColWidth := 0;
      ColsSumWidth := 0;
      ColWidthDifferenceWidth := 0;
    
      Grid.Canvas.Font.Assign(Grid.Font);
      for ACol := 0 to ColsCount - 1 do
      begin
    
        for ARow := 0 to Grid.RowCount - 1 do
        begin
          ColMinWidth := Grid.Canvas.TextWidth(Grid.Cells[ACol, ARow]);
        end;
    
        ColsSumWidth := ColsSumWidth + ColMinWidth;
        Grid.ColWidths[ACol] := ColMinWidth;
      end;
           
      if ColsSumWidth < GridWidth then
        begin
          ColWidthDifferenceWidth := (GridWidth - ColsSumWidth) div ColsCount - 1;
    
          for ACol := 0 to ColsCount - 1 do
          begin
            Grid.ColWidths[ACol] := Grid.ColWidths[ACol] + ColWidthDifferenceWidth;
          end;
        end
      else
        begin
          ColWidthDifferenceWidth := (ColsSumWidth - GridWidth) div ColsCount;
    
          for ACol := 0 to ColsCount - 1 do
          begin
            Grid.ColWidths[ACol] := Grid.ColWidths[ACol] - ColWidthDifferenceWidth;
          end;
        end;
    end;
Extrauterine answered 5/5, 2023 at 12:51 Comment(2)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Overlong
This one also works, too, it just behaves a bit differently. It will also resize the first fixed column and it will try to fill the StringGrid to its full width, whereas other solutions here will only resize based on a "min width" but will leave blank space in the overall StringGrid control.Holytide
W
0

Solution If there are more doubts command "grid.AutoFitColumns()" Where grid is one "TAdvStringGrid";

;)

Wessel answered 16/6, 2016 at 11:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.