如何使 StringGrid 的列适合网格的宽度?

问题描述 投票:0回答:5

我一直在寻找解决方案,但没有任何运气。有谁知道一个简单的方法来做到这一点?例如,我想拉伸网格的第二列以适应网格的宽度!

delphi grid width stretch
5个回答
9
投票

使用

ColWidths
属性,如下所示:

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

为了获得更强大和灵活的解决方案,请考虑所有固定列并参数化列索引:

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;

2
投票

以下代码适用于

FixedCols = 0
(以适应其他值,例如:
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;

1
投票

这样更好:

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;

0
投票

解决方案 如果还有更多疑问 命令“grid.AutoFitColumns()” 其中网格是一个“TAdvStringGrid”;

;)


0
投票

允许您根据容器的大小以及给定内容的尺寸来动态调整列的大小。

    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;
© www.soinside.com 2019 - 2024. All rights reserved.