Jump to content

Recommended Posts

Posted

Hi,

 

I was wondering if it would be possible to automatically make the fields in the grid autowidth the size of the grid?

Is this something that will be coming or should this be calculated by the application?

 

Thx,

Bernaert Dominique

  • Administrators
Posted

Hi,

 

I was wondering if it would be possible to automatically make the fields in the grid autowidth the size of the grid?

Is this something that will be coming or should this be calculated by the application?

 

Thx,

Bernaert Dominique

 

Already logged as 000555, but I failed to find a proper algorithm for doing this.

Posted

Already logged as 000555, but I failed to find a proper algorithm for doing this.

 

 

Hi,

 

I´m using the procedure above in some cases to set auto columns width of my UniDBGrid´s. Maybe can be useful. I put in the Tag property of the column if it will be self-adjusting and call the procedure through the form OnShow event.

 

 

 

 

uses Vcl.DBGrids

 

...

 

procedure FixDBGridColumnsWidth(const DBGrid: TUniDBGrid);
var
 I: Integer;
 TotWidth: Integer;
 VarWidth: Integer;
 ResizableColumnCount: Integer;
 AColumn: TUniDBGridColumn;
begin
 //total width of all columns before resize
 TotWidth := 0;

 //how many columns need to be auto-resized
 ResizableColumnCount := 0;

 for I := 0 to Pred(DBGrid.Columns.Count) do
 begin
   TotWidth := TotWidth + DBGrid.Columns[i].Width;

   if Assigned(DBGrid.Columns[i].Field) and (DBGrid.Columns[i].Tag <> 0) then
     Inc(ResizableColumnCount);
 end;

 //add 1px for the column separator line
 if dgColLines in DBGrid.Options then
   TotWidth := TotWidth + DBGrid.Columns.Count;

 //add indicator column width
 if dgIndicator in DBGrid.Options then
   TotWidth := TotWidth + IndicatorWidth;

 //width vale "left"
 VarWidth := DBGrid.Width - TotWidth;

 //Equally distribute VarWidth to all auto-resizable columns
 if ResizableColumnCount > 0 then
   VarWidth := VarWidth div ResizableColumnCount;

 for i := 0 to Pred(DBGrid.Columns.Count) do
 begin
   AColumn := DBGrid.Columns[i];

   if Assigned(AColumn.Field) and (AColumn.Tag <> 0) then
   begin
     AColumn.Width := AColumn.Width + VarWidth;

     //Tag of column can be value of minsize. This function can be called from OnResize event of form to auto adjust columns
     if AColumn.Width < AColumn.Tag then
       AColumn.Width := AColumn.Tag;
   end;
 end;
end;

 

 

...

 

 

procedure TUniForm1.UniFormShow(Sender: TObject);
begin
 FixDBGridColumnsWidth(myDBGrid);
end;

×
×
  • Create New...