Autosize columns in DBGRID

Hi

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

Thanks
zattzAsked:
Who is Participating?
 
zattzConnect With a Mentor Author Commented:
None of these work with the dataset solution im using.
0
 
Geert GOracle dbaCommented:
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
 
zattzAuthor Commented:
I couldn't get it to work
Max and FillChar arnt available in delphi.net
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
Geert GOracle dbaCommented:

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
 
zattzAuthor Commented:
It only affects one column with a double in it.

The rest of the date/string columns stay the same
0
 
Geert GOracle dbaCommented:
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
 
Geert GOracle dbaCommented:
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
 
zattzAuthor Commented:
The columns all went to zero...
0
 
senadCommented:
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
 
Geert GOracle dbaCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.