Quantcast
Channel: Delphi Forum - Delphi Programming Kings of Code - Delphi Programming
Viewing all articles
Browse latest Browse all 173

Adjust size of the columns of a DBGRID

$
0
0
procedure DBGridAjustaColunas( const Grid: TDBGrid );
var a : array of integer;
    i,
    n : integer;
    DataSet: TDataSet;
begin
  SetLength( a, Grid.Columns.Count);
  for i:= 0 to Grid.Columns.Count - 1 do
      a := Grid.Canvas.TextWidth(Grid.Columns.Title.Caption);

  DataSet:= Grid.DataSource.DataSet;
  DataSet.DisableControls;

  try
    while not DataSet.Eof do begin
          for i:= 0 to Grid.Columns.Count -1 do begin
              n:= Grid.Canvas.TextWidth( DataSet.Fields.Text );
              if n>a then a:=n;
          end;
          DataSet.Next;
    end;

    for i:= 0 to Grid.Columns.Count -1 do begin
        Grid.Columns.Width := a + 5;

        {checka espaço para colocar a imagem de ordenação na coluna}
        if Grid.Columns.Items.Width <
           Grid.Canvas.TextWidth(Grid.Columns.Title.Caption)+5 then
           Grid.Columns.Width := Grid.Columns.Width + 5;
    end;

    if not DataSet.IsEmpty then DataSet.First;

  finally
    DataSet.EnableControls;
  end;

end;

Viewing all articles
Browse latest Browse all 173