我一直在寻找一个没有运气的解决方案。有谁知道一个简单的方法吗?我想拉伸我的网格的第二列以适应网格的宽度!
答案 0 :(得分:8)
使用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;
答案 1 :(得分:0)
解决方案如果有更多疑问 命令“ grid.AutoFitColumns()” grid是一个“TAdvStringGrid”;
)
答案 2 :(得分:0)
以下代码与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;
答案 3 :(得分:0)
像这样更好:
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;