作者:Sunday老师 | 来源:互联网 | 2023-09-23 11:42
自动调整列的宽度functionDBGridRecordSize(mColumn:TColumnEh):Boolean;{返回记录数据网格列显示最大宽度是否成功}beginResu
自动调整列的宽度
function DBGridRecordSize(mColumn: TColumnEh): Boolean;
{ 返回记录数据网格列显示最大宽度是否成功 }
begin
Result := False;
if not Assigned(mColumn.Field) then Exit;
mColumn.Field.Tag := Max(mColumn.Field.Tag,
TDBGrid(mColumn.Grid).Canvas.TextWidth(mColumn.Field.DisplayText));
Result := True;
end; { DBGridRecordSize }
function DBGridAutoSize(mDBGrid: TDBGridEh; mOffset: Integer = 5): Boolean;
{ 返回数据网格自动适应宽度是否成功 }
var
I: Integer;
begin
Result := False;
if not Assigned(mDBGrid) then Exit;
if not Assigned(mDBGrid.DataSource) then Exit;
if not Assigned(mDBGrid.DataSource.DataSet) then Exit;
if not mDBGrid.DataSource.DataSet.Active then Exit;
for I := 0 to mDBGrid.Columns.Count - 1 do begin
if not mDBGrid.Columns[i].Visible then Continue;
if Assigned(mDBGrid.Columns[i].Field) then
mDBGrid.Columns[i].Width := Max(mDBGrid.Columns[i].Field.Tag,
mDBGrid.Canvas.TextWidth(mDBGrid.Columns[i].Title.Caption)) + mOffset
else mDBGrid.Columns[i].Width :=
mDBGrid.Canvas.TextWidth(mDBGrid.Columns[i].Title.Caption) + mOffset;
mDBGrid.Refresh;
end;
Result := True;
end; { DBGridAutoSize }
// 应用代码
use Math;
procedure TForm1.DBGridEh1DrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumnEh;
State: TGridDrawState);
begin
DBGridRecordSize(Column);
end;