dbgrid 列宽_如何自动修复DBGrid列宽

dbgrid 列宽

Designed to enable a user to view and edit data in a tabular grid, the DBGrid provides various ways of customizing the way it represents "its" data. With so much flexibility, a Delphi developer can always find new ways to make it more powerful.

DBGrid旨在使用户能够查看和编辑表格网格中的数据,它提供了多种自定义其表示“其”数据的方式的方式。 有了这么多的灵活性, Delphi开发人员始终可以找到使其更强大的新方法。

One of the missing features of TDBGrid is that there is no option to automatically adjust the widths of specific columns to completely fit the grid's client width. When you resize the DBGrid component at runtime, the column widths are not resized.

TDBGrid缺少的功能之一是没有选项可以自动调整特定列的宽度以完全适合网格的客户端宽度。 在运行时调整DBGrid组件的大小时,不会调整列宽的大小。

If the width of the DBGrid is larger than the total width of all the columns, you'll get an empty area right after the last column. On the other hand, if the total width of all the columns is larger than the width of the DBGrid, a horizontal scrollbar will appear.

如果DBGrid的宽度大于所有列的总宽度,则在最后一列之后会出现一个空白区域。 另一方面,如果所有列的总宽度大于DBGrid的宽度,则会出现水平滚动条。

自动调整DBGrid列宽 ( Automatically Adjust DBGrid Column Widths )

There's one handy procedure you can follow that fixes the widths of selective DBGrid columns when the grid is resized at runtime.

在运行时调整网格大小时,可以遵循一个便捷的过程来固定选择性DBGrid列的宽度。

It's important to note that, usually, only two to three columns in a DBGrid actually need to be auto-resized; all the other columns display some "static-width" data. For example, you can always specify fixed width for columns displaying values from data fields that are represented with TDateTimeField, TFloatField, TIntegerField, and similar.

需要注意的重要一点是,通常通常只需要自动调整DBGrid中的两到三列即可; 其他所有列均显示一些“静态宽度”数据。 例如,您始终可以为显示以TDateTimeField,TFloatField,TIntegerField等表示的数据字段中的值的列指定固定宽度。

What's more, you'll probably create (at design time) persistent field components using the Fields editor, to specify the fields in the dataset, their properties, and their ordering. With a TField descendant object, you can use the Tag property to indicate that a particular column displaying values for that field must be auto-sized.

此外,您可能会使用“字段”编辑器(在设计时)创建持久字段组件,以指定数据集中的字段,它们的属性及其顺序。 对于TField子代对象,可以使用Tag属性指示必须自动调整显示该字段的值的特定列。

This is the idea: If you want a column to auto-fit the available space, assign an integer value for the TField descendant's Tag property that indicates the corresponding column's minimum width.

这是一个主意:如果您希望列自动适应可用空间,请为TField后代的Tag属性分配一个整数值,该值指示相应列的最小宽度。

FixDBGridColumnsWidth过程 ( The FixDBGridColumnsWidth Procedure )

Before you begin, in the OnCreate event for the Form object containing the DBGrid, specify what columns need to be auto-resized by assigning a non-zero value for the Tag property of the corresponding TField object.

在开始之前,在包含DBGrid的Form对象的OnCreate事件中 ,通过为相应的TField对象的Tag属性分配一个非零值来指定需要自动调整大小的列。


procedure TForm1.FormCreate(Sender: TObject);
begin
//setup autoresizable columns by asigning
//Minimm Width in the Tag property.
//using fixed value: 40 px
Table1.FieldByName('FirstName').Tag := 40;//using variable value: width of the
//default Column title text
Table1.FieldByName('LastName').Tag := 4 + Canvas.TextWidth( Table1.FieldByName('LastName').DisplayName);
end
;

In the above code, Table1 is a TTable component linked to a DataSource component, which is linked with the DBGrid. The Table1.Table property points to the DBDemos Employee table.

在上面的代码中,Table1是一个TTable组件,链接到一个DataSource组件 ,该组件与DBGrid链接。 Table1.Table属性指向DBDemos Employee表。

We've marked the columns displaying the values for FirstName and LastName fields to be auto-resizable. The next step is to call our FixDBGridColumnsWidth in the OnResize event handler for the Form:

我们已将显示“名字”和“姓氏”字段值的列标记为可自动调整大小。 下一步是在OnResize事件处理程序中为Form调用FixDBGridColumnsWidth:


procedure TForm1.FormResize(Sender: TObject);
begin
FixDBGridColumnsWidth(DBGrid1);
end
;

Note: All of this makes sense if the Align property of the DBGrid includes one of following values: alTop, alBottom, alClient, or alCustom.

注意:如果DBGrid的Align属性包括以下值之一,则所有这些都有意义:alTop,alBottom,alClient或alCustom。

Finally, here's the FixDBGridColumnsWidth procedure's code:

最后,这是FixDBGridColumnsWidth过程的代码:


procedure FixDBGridColumnsWidth(const DBGrid: TDBGrid);
var
i : integer; TotWidth : integer; VarWidth : integer; ResizableColumnCount : integer; AColumn : TColumn;
begin
//total width of all columns before resize
TotWidth := 0;//how to divide any extra space in the grid
VarWidth := 0;//how many columns need to be auto-resized
ResizableColumnCount := 0;for i := 0 to -1 + DBGrid.Columns.Count dobegin
TotWidth := TotWidth + DBGrid.Columns[i].Width;if DBGrid.Columns[i].Field.Tag 0 then
Inc(ResizableColumnCount);end;//add 1px for the column separator lineif dgColLines in DBGrid.Options then
TotWidth := TotWidth + DBGrid.Columns.Count;//add indicator column widthif dgIndicator in DBGrid.Options then
TotWidth := TotWidth + IndicatorWidth;//width vale "left"
VarWidth := DBGrid.ClientWidth - TotWidth;//Equally distribute VarWidth
//to all auto-resizable columns
if ResizableColumnCount > 0 then
VarWidth := varWidth div ResizableColumnCount;for i := 0 to -1 + DBGrid.Columns.Count dobegin
AColumn := DBGrid.Columns[i];if AColumn.Field.Tag 0 thenbegin
AColumn.Width := AColumn.Width + VarWidth;if AColumn.Width then
AColumn.Width := AColumn.Field.Tag;end;end;
end
; (*FixDBGridColumnsWidth*)

翻译自: https://www.thoughtco.com/auto-fix-dbgrid-column-widths-4077417

dbgrid 列宽

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值