How to auto fit/scale DBGrid's (or other similar) columns widths according to its contents?
Asked Answered
F

8

10

I am trying to make a frame with a DBGrid that will serve for more than 10 tables with half of its fields as defaults, and other fields exclusive for each table.

As the space for the columns are limited and I do not want to configure each column of each table manually because it is very poor quality work, I was wondering a way to calculate the width of each column by the largest content of a row inside that column, measured by the own component or by the data set.

Does anyone knows the way? Is there out in the world some custom component with that power? I need a solution that implements increase and decrease of size, according to the the visible data in the all visible columns of the grid. My solution so far had a problem with the painting of the selected cell, witch jumps out of the selected dataset row.

enter image description here


Note: Please, do not close my question. It is not about the fit with the grid's width or the form's width. It is about all the columns width to minimize the horizontal scrollbar, but not necessarily hide it.

Floorage answered 8/11, 2012 at 16:10 Comment(3)
@RRUZ it is not fit to the form, but in its own content, even if horizontal scrollbar get visible.Floorage
I know I've asked this before and got a really good answer...Boudreau
@kobik The link is dead.Nashbar
T
11

What you have to do is to use the grid's canvas to measure the contents of each column and set the column's width accordingly. You can either iterate through the dataset or use the OnColumnDraw-Event to adjust the width on the fly.

Here's a sample (I had to use an offset of 5 pixels)

procedure TForm7.DBGridDrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
Var
  w : Integer;

begin
  w := 5+DBGrid.Canvas.TextExtent(Column.Field.DisplayText).cx;
  if w>column.Width then Column.Width := w;
end;

procedure TForm7.FormActivate(Sender: TObject);
Var
  i : Integer;

begin
  // Initialize width
  for I := 0 to DBGrid.Columns.Count - 1 do
    DBGrid.Columns[i].Width := 5 + DBGrid.Canvas.TextWidth(DBGrid.Columns[i].title.caption)
end;
Trillion answered 8/11, 2012 at 18:8 Comment(7)
This answer was so in the face. How stupid am I? Thank you very much!Floorage
I will use a Tag to check the biggest width so I can downsize the column when I need.Floorage
Hmm. my answer got 'unaccepted' recently. I would like to know why.Trillion
#13298008 I am getting that problem when I use your code. If by tomorrow I find no other answer I check back yours. I really need that done to get on with my project ;(Floorage
Ah, ok. I left a comment on the other post. It might help.Trillion
I had this problem too but I use C++ Builder XE instead Delphi. I try to manually convert this code to BCBXE language but I don't understand with this line : w := 5+DBGrid.Canvas.TextExtent(Column.Field.DisplayText).cx; Can you describe to me how to do that in C++ Builder XE? Thanks in advance.Authorized
I don't use C++, sorry. But it should be quite obvious.Trillion
T
9

EDITED:

My first code was about fit the columns inside the grid with, with this new code, AutoSizeColumns reads the records to calc the width of each column until MaxRows or Dataset.Eof:

class function TDBGridHelper.AutoSizeColumns(DBGrid: TDBGrid; const MaxRows: Integer = 25): Integer;

var
  DataSet: TDataSet;
  Bookmark: TBookmark;
  Count, I: Integer;
  ColumnsWidth: array of Integer;
begin
  SetLength(ColumnsWidth, DBGrid.Columns.Count);
  for I := 0 to DBGrid.Columns.Count - 1 do
    if DBGrid.Columns[I].Visible then
      ColumnsWidth[I] := DBGrid.Canvas.TextWidth(DBGrid.Columns[I].Title.Caption + '   ')
    else
      ColumnsWidth[I] := 0;
  if DBGrid.DataSource <> nil then
    DataSet := DBGrid.DataSource.DataSet
  else
    DataSet := nil;
  if (DataSet <> nil) and DataSet.Active then
  begin
    Bookmark := DataSet.GetBookmark;
    DataSet.DisableControls;
    try
      Count := 0;
      DataSet.First;
      while not DataSet.Eof and (Count < MaxRows) do
      begin
        for I := 0 to DBGrid.Columns.Count - 1 do
          if DBGrid.Columns[I].Visible then
            ColumnsWidth[I] := Max(ColumnsWidth[I], DBGrid.Canvas.TextWidth(
              DBGrid.Columns[I].Field.Text));
        Inc(Count);
        DataSet.Next;
      end;
    finally
      DataSet.GotoBookmark(Bookmark);
      DataSet.FreeBookmark(Bookmark);
      DataSet.EnableControls;
    end;
  end;
  Count := 0;
  for I := 0 to DBGrid.Columns.Count - 1 do
    if DBGrid.Columns[I].Visible then
    begin
      DBGrid.Columns[I].Width := ColumnsWidth[I];
      Inc(Count, ColumnsWidth[I]);
    end;
  Result := Count - DBGrid.ClientWidth;
end;

I call it in the DataSet.AfterOpen event:

TGridHelper.AutoSizeColumns(MyDBGrid);
Tenor answered 8/11, 2012 at 16:40 Comment(9)
Unfortunately this solution does not fit to my needs. In this code you stretch the columns so they do not exceed the grid area, but the content is compromised because some of the data gets cut. The correct is to fit the text content and not the total column width with the total grid width. But Thanks for Trying.Floorage
This is not an answer to the question.Kallman
The negative is not fair, since the OP did not explain what he really wants first, now I edited my post to match the needs with another code from the same unit.Tenor
@NGLN, the downvote is not fair. My answer was based in the original post of the OP, after he edited and explain better what is the real need, I update my post.Tenor
@Cesar Downvote wasn't mine. Besides, question was crystal clear from the beginning.Kallman
@CesarRomero, your question is now good, but I think the one from alzaimar fits because I am using a remote database and checking the size of data set may slow the system. But I will test it now.Floorage
+1; excellent answer which I found after doing a very analogous implementation. Minor differences are that by default I use the RowCount of the DbGrid to limit the rows to check (I do that because those records have already been fetched from the database) unless it is a TClientDataSet where I scan all records (they are in-memory anyway).Persian
Just that a class helper can not be called like this.Bartlett
Uses the System.Math unit and if you don't have a field assigned to a column you will get an exception (easy to fix, I have a few columns that I render myself)Ophthalmitis
M
2

Why to use so complicated code? :D Just use this. Its also have 5 px offset.

procedure TForm1.FormActivate(Sender: TObject);

var

  i:integer;

begin

  for i :=0 to DbGrid1.Columns.Count - 1 do
   DbGrid1.Columns[i].width :=5+dbgrid1.Canvas.TextWidth(DbGrid1.Columns[i].Title.Caption);

end;
Mg answered 1/5, 2014 at 6:48 Comment(0)
W
1

For each column you want to be auto sized set the property SizePriority=1 and for fixed columns set SizePriority=0, in your case only the last column will be auto sized.

Then set property grid.AutoFillColumns=true and that should do it.

Wrought answered 14/1, 2016 at 15:18 Comment(0)
C
1

For TRUEDBGRID .net you can do this:

 Private Sub AutoSizeGrid(Grid As C1.Win.C1TrueDBGrid.C1TrueDBGrid)
        For Each Sp As C1.Win.C1TrueDBGrid.Split In Grid.Splits
            For Each Cl As C1.Win.C1TrueDBGrid.C1DisplayColumn In Sp.DisplayColumns
                Cl.AutoSize()
            Next
        Next
End Sub

For TrueDbGrid ActiveX in vb60 this (this code not include splits):

Public Function APEXGridAutoFix(Grid As TrueOleDBGrid80.TDBGrid)
Dim Col As TrueOleDBGrid80.Column
For Each Col In Grid.Columns
    Col.AutoSize
Next
End Function
Carillonneur answered 18/10, 2018 at 17:36 Comment(0)
F
0

This solution makes all columns expand or shrink according to it's contents, without care if there has to be scroll bars or not, and fix the selected cell drawing malfunction and the the record pointer malfunction.

type
  TColumnAutoAdjust = record {Save the information responsible for setting column widths in the grid}
    Field: String;           {Field name whose information is being stored}
    Registered: Boolean;     {Indicates whether the size of this column already registered}
    Updated: Boolean;        {Indicates the actual size of the column was updated}
    LastWidth: Integer;      {Width indicates the final text of a record of a row column}
    CurrWidth: Integer;      {Indicates the current size and column width}
    Reverter: Integer;       {Indicates the greatest width recorded but that is less than the current}
    Scrolls: Integer;        {Indicates the amount of scrolls present after one width adjustment}
    RecNo: Integer;          {Indicates which was the record in the table which increased the width of colune}
  end;

var { inside the forms private }
  gdCols: array of TColumnAutoAdjust; { vetor de ajuste de largura de cada coluna na grade de resultado }
  RegisteredCols: Integer; { quantas colunas já foram registradas no controle de ajuste }
  gdVisibleRows: Integer; { quantas linhas de cadastros estão visíveis da grade de resultado }
  gdVisibleCols: Integer; { quantas colunas de cadastros estão visíveis da grade de resultado }

{ before showing the grid }  
    RegisteredCols := ResultGrid.Columns.Count;
    SetLength(gdCols, RegisteredCols); { determina o tamanho da vetor de controle de colunas }
    { libera a lista }
    ResultGrid.Align := alClient;
    for i := 0 to RegisteredCols -1 do { inicializando a largura das colunas no tamanho do título de cada }
    begin
      gdCols[i].Field := ResultGrid.Columns[i].FieldName;
      ResultGrid.Columns[i].Width := ResultGrid.Canvas.TextExtent(ResultGrid.Columns[i].Title.Caption).cx;
      ResultGrid.Columns[i].Alignment := taLeftJustify;
      ResultGrid.Columns[i].Title.Alignment := taLeftJustify;
    end;
    BrowserQuery.Open;
    ResultGrid.Show;
    for i := 0 to gdVisibleRows do
    begin
      BrowserQuery.Next;
      ResultGrid.Refresh;
    end;
    for i := 0 to gdVisibleRows do
    begin
      BrowserQuery.Prior;
      ResultGrid.Refresh;
    end;
    BrowserQuery.First;
    ResultGrid.SetFocus;
  end

{ after dataset scroll}      
procedure TRecordsBrowserFrameBase.BrowserQueryAfterScroll(DataSet: TDataSet);
var
  i, TitleWidth: Integer;
  mayAdjustAgain: Boolean; {  }
begin
{ ajusta as colunas da grade de resultado a cada movimento da tabela de resultado }
  mayAdjustAgain := False;
  for i := 0 to RegisteredCols -1 do
  begin
    if not gdCols[i].Updated then
    begin
      ResultGrid.Columns[i].Width := gdCols[i].CurrWidth;
      gdCols[i].Scrolls := 0;
      gdCols[i].Updated := True;
    end
    else
    begin
      Inc(gdCols[i].Scrolls);
      if (DataSet.RecNo > gdCols[i].RecNo + gdVisibleRows) or (DataSet.RecNo < gdCols[i].RecNo - gdVisibleRows) then
      begin
        TitleWidth := MaxColSpacing + ResultGrid.Canvas.TextExtent(ResultGrid.Columns[i].Title.Caption).cx;
        gdCols[i].LastWidth := gdCols[i].CurrWidth;
        gdCols[i].CurrWidth := IFX(gdCols[i].Reverter > TitleWidth, gdCols[i].Reverter, TitleWidth);
        gdCols[i].Reverter := IFX(gdCols[i].Reverter > TitleWidth, TitleWidth, 0);
        gdCols[i].Updated := False;
        mayAdjustAgain := True;
      end;
    end;
  end;
  if mayAdjustAgain then
  begin
    ResultGrid.Refresh;
    BrowserQueryAfterScroll(DataSet);
  end;
end;

{ on draw column cell }

procedure TRecordsBrowserFrameBase.GridColumnWidthAdjust(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
  ColWidth, TextWidth, TitleWidth: Integer;
begin
{ ajusta a capitalização do texto das células }
  (Sender as TJvDBGrid).Canvas.Pen.Color := clWhite;
  (Sender as TJvDBGrid).Canvas.Rectangle(Rect);
  (Sender as TJvDBGrid).Canvas.TextOut(Rect.Left+2, Rect.Top+2, NameCase(Column.Field.DisplayText));
{ ajusta as colunas de uma grade de acordo com o conteúdo das células }
  gdVisibleRows := (Sender as TJvDBGrid).VisibleRowCount;
  gdVisibleCols := (Sender as TJvDBGrid).VisibleColCount;
  TitleWidth := MaxColSpacing + (Sender as TJvDBGrid).Canvas.TextExtent(Column.Title.Caption).cx;
  TextWidth := MaxColSpacing + (Sender as TJvDBGrid).Canvas.TextExtent(NameCase(Column.Field.DisplayText)).cx;
  ColWidth := Column.Width;
  {$WARNINGS OFF}
  if (TextWidth > gdCols[DataCol].Reverter) and (TextWidth < ColWidth) then gdCols[DataCol].Reverter := TextWidth;
  if (TextWidth > ColWidth) then { texto da célula é mais largo que a coluna }
  begin
    gdCols[DataCol].Registered := True;
    gdCols[DataCol].LastWidth := ColWidth;
    gdCols[DataCol].CurrWidth := TextWidth;
    gdCols[DataCol].Updated := False;
    gdCols[DataCol].RecNo := BrowserQuery.RecNo;
    gdCols[DataCol].Reverter := TitleWidth;
    Exit;
  end;
  if (ColWidth < TitleWidth) then { texto da célula é menor que o título da coluna }
  begin
    gdCols[DataCol].Registered := True;
    gdCols[DataCol].LastWidth := ColWidth;
    gdCols[DataCol].CurrWidth := TitleWidth;
    gdCols[DataCol].Updated := False;
    gdCols[DataCol].Reverter := TitleWidth;
    Exit;
  end;
{$WARNINGS ON}
end;
Floorage answered 19/11, 2012 at 22:16 Comment(0)
U
-1

Hello use this procedure.

Procedure AutoSizeColDBGrid(DBGrid:TDBGrid);
var i, ColWidth, ColTextWidth:integer;
begin
 if DBGrid.DataSource.DataSet.Active then
   begin
     DBGrid.DataSource.DataSet.DisableControls;
     for i:= 0 to DBGrid.Columns.Count-1 do
       begin
         ColWidth:=DBGrid.Canvas.TextWidth(DBGrid.Columns[i].Field.DisplayLabel);
         DBGrid.DataSource.DataSet.First;
       while not DBGrid.DataSource.DataSet.EOF do
        begin
       ColTextWidth:=DBGrid.Canvas.TextWidth(DBGrid.Columns[i].Field.DisplayText);
          if (ColTextWidth > ColWidth) then
            begin
              ColWidth := ColTextWidth;
            end;
          DBGrid.DataSource.DataSet.Next;
       end;{while}
       DBGrid.Columns[i].Width:=ColWidth+10;
     end;{for}
DBGrid.DataSource.DataSet.EnableControls;
DBGrid.DataSource.DataSet.First;
end;
end;
Ut answered 17/5, 2019 at 2:28 Comment(0)
G
-2

Use procedure DbGrid1.AutoAdjustColumns and that's it.

Glomerulus answered 22/2, 2019 at 17:42 Comment(3)
Is that a new procedure or did it exist on older versions of Delphi?Floorage
There is no such method in Delphi at last in latest versionInvestment
edn.embarcadero.com/article/27548 There's a procedure published on the Embacadero Developer community - see above.Creaky

© 2022 - 2024 — McMap. All rights reserved.