?
Solved

Autosize columns in DBGRID

Posted on 2009-02-17
10
Medium Priority
?
2,137 Views
Last Modified: 2013-11-23
Hi

How can I get a DBGRID to autosize each column to the maximum length of all rows in that column?

Thanks
0
Comment
Question by:zattz
  • 5
  • 4
10 Comments
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 23659671
something like this ?
procedure TForm1.Query1AfterOpen(DataSet: TDataSet);
var I: Integer;
  cw: array of integer;
  b: TBookmark;
  grid: TDBGrid;
begin
  SetLength(cw, Dataset.FieldCount);
  FillChar(cw, Dataset.FieldCount, 0);
  grid := DBGrid1;
  Dataset.DisableControls;
  try
    b := Dataset.GetBookmark;
    try
      Dataset.First;
      while not Dataset.Eof do
      begin
        for I := 0 to Dataset.FieldCount-1 do
          cw[I] := Max(cw[I], grid.Canvas.TextWidth(Dataset.Fields.Fields[I].AsString));
        Dataset.Next;
      end;
      for I := 0 to Dataset.FieldCount-1 do
        Dataset.Fields.Fields[I].DisplayWidth := cw[I];
      if Dataset.BookmarkValid(b) then
        Dataset.GotoBookmark(b);
    finally
      Dataset.FreeBookmark(b);
    end;
  finally
    DataSet.EnableControls;
  end;
end;

Open in new window

0
 

Author Comment

by:zattz
ID: 23660618
I couldn't get it to work
Max and FillChar arnt available in delphi.net
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 23660637

procedure TForm1.Query1AfterOpen(DataSet: TDataSet);
var I: Integer;
  cw: array of integer;
  b: TBookmark;
  grid: TDBGrid;
begin
  SetLength(cw, Dataset.FieldCount);
  for I := 0 to Dataset.FieldCount-1 do 
    cw[I] := 0;
  grid := DBGrid1;
  Dataset.DisableControls;
  try
    b := Dataset.GetBookmark;
    try
      Dataset.First;
      while not Dataset.Eof do
      begin
        for I := 0 to Dataset.FieldCount-1 do
          if cw[I] < grid.Canvas.TextWidth(Dataset.Fields.Fields[I].AsString) then 
            cw[I] := grid.Canvas.TextWidth(Dataset.Fields.Fields[I].AsString);
        Dataset.Next;
      end;
      for I := 0 to Dataset.FieldCount-1 do
        Dataset.Fields.Fields[I].DisplayWidth := cw[I];
      if Dataset.BookmarkValid(b) then
        Dataset.GotoBookmark(b);
    finally
      Dataset.FreeBookmark(b);
    end;
  finally
    DataSet.EnableControls;
  end;
end;

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:zattz
ID: 23660746
It only affects one column with a double in it.

The rest of the date/string columns stay the same
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 23660768
so this would be the culprit :
      for I := 0 to Dataset.FieldCount-1 do
        Dataset.Fields.Fields[I].DisplayWidth := cw[I];

we need to translate the Dataset columns to grid Columns ...
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 23660829
change previous snippet to this snippet below
var J: Integer;
 
for I := 0 to Dataset.FieldCount-1 do
  for J := 0 to grid.Columns.Count-1 do 
    if grid.Columns[J].FieldName = Dataset.Fields.Fields[I].FieldName then 
      grid.Columns[J].Width := cw[I];

Open in new window

0
 

Author Comment

by:zattz
ID: 23669704
The columns all went to zero...
0
 
LVL 22

Expert Comment

by:senad
ID: 23768435
I have not used net before but are not column width decided by table/queery component ?
the grid should display adequate width by force.
also making the table/queery component active in design time allows you to stretch column width manually to desired lenght (which is better than allowing the table/queery component to do so by themselves)
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 23795808
you did like this ?
procedure TForm1.Query1AfterOpen(DataSet: TDataSet);
var I, J: Integer;
  cw: array of integer;
  b: TBookmark;
  grid: TDBGrid;
begin
  SetLength(cw, Dataset.FieldCount);
  for I := 0 to Dataset.FieldCount-1 do 
    cw[I] := 0;
  grid := DBGrid1;
  Dataset.DisableControls;
  try
    if Dataset.RecordCount > 0 then 
    begin
      b := Dataset.GetBookmark;
      try
        Dataset.First;
        while not Dataset.Eof do
        begin
          for I := 0 to Dataset.FieldCount-1 do
            if cw[I] < grid.Canvas.TextWidth(Dataset.Fields.Fields[I].AsString) then 
              cw[I] := grid.Canvas.TextWidth(Dataset.Fields.Fields[I].AsString);
          Dataset.Next;
        end;
        for I := 0 to Dataset.FieldCount-1 do
          for J := 0 to grid.Columns.Count-1 do 
            if grid.Columns[J].FieldName = Dataset.Fields.Fields[I].FieldName then 
              grid.Columns[J].Width := cw[I];
        if Dataset.BookmarkValid(b) then
          Dataset.GotoBookmark(b);
      finally
        Dataset.FreeBookmark(b);
      end;
    end;
  finally
    DataSet.EnableControls;
  end;
end;

Open in new window

0
 

Accepted Solution

by:
zattz earned 0 total points
ID: 23948763
None of these work with the dataset solution im using.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question