gpt4 book ai didi

delphi - 调整列宽 DBGrid

转载 作者:行者123 更新时间:2023-12-03 14:46:02 25 4
gpt4 key购买 nike

我有一个 TDBGrid。它有效,但显示的列非常大。

如何设置“自动修复列宽”?

最佳答案

所需的列宽取决于网格 Canvas 的设置和每个字段的显示文本的最大长度。

procedure FitGrid(Grid: TDBGrid);
const
C_Add=3;
var
ds: TDataSet;
bm: TBookmark;
i: Integer;
w: Integer;
a: Array of Integer;
begin
ds := Grid.DataSource.DataSet;
if Assigned(ds) then
begin
ds.DisableControls;
bm := ds.GetBookmark;
try
ds.First;
SetLength(a, Grid.Columns.Count);
while not ds.Eof do
begin
for I := 0 to Grid.Columns.Count - 1 do
begin
if Assigned(Grid.Columns[i].Field) then
begin
w := Grid.Canvas.TextWidth(ds.FieldByName(Grid.Columns[i].Field.FieldName).DisplayText);
if a[i] < w then
a[i] := w ;
end;
end;
ds.Next;
end;
for I := 0 to Grid.Columns.Count - 1 do
Grid.Columns[i].Width := a[i] + C_Add;
ds.GotoBookmark(bm);
finally
ds.FreeBookmark(bm);
ds.EnableControls;
end;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
FitGrid(DBgrid1)
end;

关于delphi - 调整列宽 DBGrid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17509924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com