Most programming enthusiasts choose and use Delphi to write software that is attracted to the rich and powerful VCL controls. Delphi Data Aware (Data-aware) control, but also become the development of MIS software programmers preferred. In so many data-aware control, Tdbgrid because of its easy to use, display information to become the most eye-catching one, a large number of domestic and foreign software has appeared its figure. Perhaps because of the use of more people, the expectations for it will be higher, and always pursue the infinite trend of the perfect realm of programmers will not miss every opportunity to improve tdbgrid.
Users who often use tdbgrid know that it does not offer a choice that allows us to match each column to the width of the data being displayed. This allows the user to manually adjust each column while the program is running, and to do the same work again the next time the program is run (Figure 1).
After making the changes, we can get a more intelligent Tdbgrid, if the width of all the columns in the Tdbgrid is greater than the width of its own, then the intelligent Tdbgrid will leave a blank on the right side of the last column it contains (see Figure 2) Conversely, if the width of all the columns in the Tdbgrid is less than the width of its own, a horizontal scroll bar appears in the lower part of the column, and the user can drag the scroll bar around to display more content.
Solution:
In this article I will provide a simple process (Procedure) to solve the problem above, which automatically fixes the width of the columns displayed in the Tdbgrid when the program is run.
First, add the following code to the Tform OnCreate event:
Procedure TForm1.FormCreate(Sender: TObject);
begin
//在Tag属性中设置需要自动调整的列的最小宽度(固定值)
//这里将列宽值设为40px
Table1.FieldByName('FirstName').Tag := 40;
//这里设置一个变化的值
//该值是做过运算的列标题的宽度值
Table1.FieldByName('LastName').Tag := 4 + Canvas.TextWidth( Table1.FieldByName('LastName').DisplayName);
end;
Second, one of the most critical processes (Procedure) is to use it to control the width of the column:
Procedure fixdbgridcolumnswidth (const dbgrid:tdbgrid);
Var
I:integer;
totwidth:integer;//defines the entire width
varwidth:integer;//defines the width of the change
resizablecolumncount:integer;//defines the total number of variable width columns
Acolumn:tcolumn;
Begin
Width of all columns before resizing
Totwidth: = 0;
Varwidth: = 0;
How many columns need to be adjusted automatically
Resizablecolumncount: = 0;
For I: = 0 to-1 + DBGrid.Columns.Count do
Begin
Totwidth: = Totwidth + dbgrid.columns[i]. Width;
If Dbgrid.columns[i]. Field.tag <> 0 Then
INC (Resizablecolumncount);
End
Add 1PX to each column divider
If dgcollines in Dbgrid.options then
Totwidth: = Totwidth + DBGrid.Columns.Count;
If dgindicator in Dbgrid.options then
Totwidth: = Totwidth + indicatorwidth;
Varwidth: = Dbgrid.clientwidth-totwidth;
The value of the average distribution change width
Give all columns that need to be adjusted automatically
If Resizablecolumncount > 0 Then
Varwidth: = varwidth div resizablecolumncount;
For I: = 0 to-1 + DBGrid.Columns.Count do
Begin
Acolumn: = Dbgrid.columns[i];
If AColumn.Field.Tag <> 0 Then
Begin
Acolumn.width: = Acolumn.width + varwidth;
If Acolumn.width < AColumn.Field.Tag Then
Acolumn.width: = AColumn.Field.Tag;
End
End
End
Finally, apply this function:
Procedure TForm1.FormResize(Sender: TObject);
begin
FixDBGridColumnsWidth(DBGrid1);
end;
The above a simple function to solve the problem of the data column often encountered, when the problem with a lot of thinking, and not blindly to find third-party controls, so that long-term adherence will really improve their level, will be from a program plasterer into a real software designer.