How to auto fit/scale DBGrid's (or other similar) columns widths according to its contents?
Solution 1
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;
Solution 2
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);
Solution 3
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;
Solution 4
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.
Solution 5
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
NaN
Developer since 1997, inspired by the possibility of creation and to help others with these creations. Interested in projects that may no just make some money, but most if it may help many people and increase the technology and human development.
Updated on May 17, 2020Comments
-
NaN almost 4 years
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.
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.
-
NaN over 11 yearsUnfortunately 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.
-
NGLN over 11 yearsThis is not an answer to the question.
-
Cesar Romero over 11 yearsThe 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.
-
Cesar Romero over 11 years@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.
-
NGLN over 11 years@Cesar Downvote wasn't mine. Besides, question was crystal clear from the beginning.
-
NaN over 11 yearsThis answer was so in the face. How stupid am I? Thank you very much!
-
NaN over 11 years@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.
-
NaN over 11 yearsI will use a Tag to check the biggest width so I can downsize the column when I need.
-
alzaimar over 11 yearsHmm. my answer got 'unaccepted' recently. I would like to know why.
-
NaN over 11 yearsstackoverflow.com/questions/13297508/… 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 ;(
-
alzaimar over 11 yearsAh, ok. I left a comment on the other post. It might help.
-
Andromeda about 11 yearsI 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.
-
alzaimar about 11 yearsI don't use C++, sorry. But it should be quite obvious.
-
Jeroen Wiert Pluimers over 10 years+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).
-
RBA over 8 yearsJust that a class helper can not be called like this.
-
NaN about 5 yearsIs that a new procedure or did it exist on older versions of Delphi?
-
M.J.Ahmadi about 5 yearsThere is no such method in Delphi at last in latest version
-
Michél Picker over 3 yearsedn.embarcadero.com/article/27548 There's a procedure published on the Embacadero Developer community - see above.
-
Alister about 2 yearsUses 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)