Einzelnen Beitrag anzeigen

Delphi.Narium

Registriert seit: 27. Nov 2017
2.490 Beiträge
 
Delphi 7 Professional
 
#12

AW: DBGrid1.Columns[0].Width:= .... / geht das auch automatisch?

  Alt 26. Nov 2021, 14:26
Schön, dann ändern wir auch das:
Delphi-Quellcode:
procedure AutoSizeColumns(AGrid : TDBGrid);
var
  i : Integer;
  LColumn : TColumn;
  LCanvas : TCanvas;
  LField : TField;
begin
  LCanvas := AGrid.Canvas;
  for i := 0 to AGrid.Columns.Count - 1 do
  begin
    LColumn := AGrid.Columns[i];
    LField := LColumn.Field;
    case LField.DataType of
      ftString, ftWideString :
      begin
        LColumn.Width := LCanvas.TextWidth(LField.AsString);
        LColumn.Alignment := taLeftJustify;
      end;
      ftSmallint, ftInteger, ftWord, ftAutoInc, ftLargeint :
      begin
        LColumn.Width := LCanvas.TextWidth(Format('%6.0n',[LField.AsFloat]));
        LColumn.Alignment := taRightJustify;
      end;
      ftFloat, ftCurrency :
      begin
        LColumn.Width := LCanvas.TextWidth(Format('%6.2n',[LField.AsFloat]));
        LColumn.Alignment := taRightJustify;
      end;
      ftDate :
      begin
        LColumn.Width := LCanvas.TextWidth(FormatDateTime('dd.mm:yyyy', LField.AsDateTime]));
        LColumn.Alignment := taLeftJustify;
      end;
      ftTime :
      begin
        LColumn.Width := LCanvas.TextWidth(FormatDateTime('hh:nn:ss', LField.AsDateTime]));
        LColumn.Alignment := taLeftJustify;
      end;
      ftDateTime :
      begin
        LColumn.Width := LCanvas.TextWidth(FormatDateTime('dd.mm.yyyy hh:nn:ss', LField.AsDateTime]));
        LColumn.Alignment := taLeftJustify;
      end;
    else
      LColumn.Width := LCanvas.TextWidth(LField.AsString);
      LColumn.Alignment := taCenter;
    end;
  end;
end;
// Aufruf:
AutoSizeColumns(DBGrid1);
Sieht dann sogar lesbarer aus.
Ob's damit schneller wird, mag und will ich nicht beurteilen.
  Mit Zitat antworten Zitat