Link to home
Create AccountLog in
Delphi

Delphi

--

Questions

--

Followers

Top Experts

Avatar of K Feening
K Feening🇦🇺

Delphi Resizing
I have a delphi 5 program that I use GTResizer to resize the form it works ok on the normal form but where I have a TDBGrid it resizes the data but not the grid
attached are some gif files to show Master and Master2 the resizing works
Master1 and master show the problem with the TDBGrid the grid headings and columns dont resize any suggestions

Master.gif
Master1.gif
Master2.gif
Master3.gif

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of Emmanuel PASQUIEREmmanuel PASQUIER🇫🇷

You have to do it yourself, on the onResize event of your Grid parent

Use the ColWidth property to set the width of each individual columns
This algo is crude by the resize of each column proportionally, but that's the idea, you can do whatever you want from that, like have some columns with a fixed size...
procedure TForm1.FormResize(Sender: TObject);
Var
 i,TotCol:Integer;
begin
 TotCol:=0;
 for i:=0 to ColCount-1 do TotCol:=TotCol+sg1.ColWidths[i];
 for i:=0 to ColCount-1 do sg1.ColWidths[i]:=Trunc(sg1.ColWidths[i]/TotCol*sg1.Width);
end;

Open in new window


Avatar of K FeeningK Feening🇦🇺

ASKER

The ColCount etc is for a tString Grid I am using a data TDBGrid is there any options for this  

Avatar of twinsofttwinsoft🇬🇷

Hi, use this function to automatically resize the columns of the DBGrid

procedure TForm1.AdjustColumnWidths(DBGrid: TDBGrid);
var
  TotalColumnWidth, ColumnCount, GridClientWidth, Filler, i: Integer;
begin
  ColumnCount := DBGrid.Columns.Count;
  if ColumnCount = 0 then
    Exit;
  TotalColumnWidth := 0;
  for i := 0 to ColumnCount - 1 do
    TotalColumnWidth := TotalColumnWidth + DBGrid.Columns[i].Width;
  if dgColLines in DBGrid.Options then
    TotalColumnWidth := TotalColumnWidth + ColumnCount;
  GridClientWidth := DBGrid.Width - GetSystemMetrics(SM_CXVSCROLL);
  if dgIndicator in DBGrid.Options then
  begin
    GridClientWidth := GridClientWidth - IndicatorWidth;
    if dgColLines in DBGrid.Options then
      Dec(GridClientWidth);
  end;
  if DBGrid.BorderStyle = bsSingle then
  begin
    if DBGrid.Ctl3D then
      GridClientWidth := GridClientWidth - 4
    else
      GridClientWidth := GridClientWidth - 2;
  end;
  if TotalColumnWidth < GridClientWidth then
  begin
    Filler := (GridClientWidth - TotalColumnWidth) div ColumnCount;
    for i := 0 to ColumnCount - 1 do
      DBGrid.Columns[i].Width := DBGrid.Columns[i].Width + Filler;
  end
  else if TotalColumnWidth > GridClientWidth then
  begin
    Filler := (TotalColumnWidth - GridClientWidth) div ColumnCount;
    if (TotalColumnWidth - GridClientWidth) mod ColumnCount <> 0 then
      Inc(Filler);
    for i := 0 to ColumnCount - 1 do
      DBGrid.Columns[i].Width := DBGrid.Columns[i].Width - Filler;
  end;
end;

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of twinsofttwinsoft🇬🇷

Hi, use it like this, as @epasquier suggested too...

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

Avatar of K FeeningK Feening🇦🇺

ASKER

Hi I added the code and the tForm1.resize code and changed the name DBGrid1 to GrdAuto which is the TDBGrid name and I makes no change to the grid column sizes

Avatar of twinsofttwinsoft🇬🇷

Hi, make sure that in the Anchors property of the dbgid both akLeft and akRight properties are set to true. I' ve tested the code and it works fine with me...

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of K FeeningK Feening🇦🇺

ASKER

Im using Delphi 5 and all the anchor properties are set to true
ive tried setting the bottom top to false and any other combination

ASKER CERTIFIED SOLUTION
Avatar of twinsofttwinsoft🇬🇷

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of K FeeningK Feening🇦🇺

ASKER

Still not working but I am upgrading to delphi 10
Delphi

Delphi

--

Questions

--

Followers

Top Experts

Delphi is the most powerful Object Pascal IDE and component library for cross-platform Native App Development with flexible Cloud services and broad IoT connectivity. It provides powerful VCL controls for Windows 10 and enables FMX development for Windows, Mac and Mobile. Delphi is your choice for ultrafast Enterprise Strong Developmentâ„¢. Look for increased memory for large projects, extended multi-monitor support, improved Object Inspector and much more. Delphi is 5x faster for development and deployment across multiple desktop, mobile, cloud and database platforms including 32-bit and 64-bit Windows 10.