KEMBAR78
Delphi DBGrid Column Auto-Resize | PDF | Software | Computer Data
0% found this document useful (0 votes)
149 views2 pages

Delphi DBGrid Column Auto-Resize

The document describes how to automatically adjust the widths of columns in a DBGrid component in Delphi when the grid is resized at runtime. It involves assigning a non-zero value to the Tag property of corresponding field components to mark them as resizable. When the form resizes, a FixDBGridColumnsWidth procedure is called that calculates the available width and equally distributes it among the tagged resizable columns. The procedure loops through the columns, adjusts the widths of those with a non-zero tag, and ensures they are not less than the tag value.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
149 views2 pages

Delphi DBGrid Column Auto-Resize

The document describes how to automatically adjust the widths of columns in a DBGrid component in Delphi when the grid is resized at runtime. It involves assigning a non-zero value to the Tag property of corresponding field components to mark them as resizable. When the form resizes, a FixDBGridColumnsWidth procedure is called that calculates the available width and equally distributes it among the tagged resizable columns. The procedure loops through the columns, adjusts the widths of those with a non-zero tag, and ensures they are not less than the tag value.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

09/04/2020 https://www.thoughtco.com/auto-fix-dbgrid-column-widths-4077417?

print

Home

How to Fix DBGrid Column Widths Automatically


By Zarko Gajic
Updated February 17, 2019

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.

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.

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.

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.

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.

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.

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.

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.

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.

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:

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.

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

https://www.thoughtco.com/auto-fix-dbgrid-column-widths-4077417?print 1/2
09/04/2020 https://www.thoughtco.com/auto-fix-dbgrid-column-widths-4077417?print

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 columnsif 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?print 2/2

You might also like