Naja, dann machen wir mal weiter und ergänzen die Routine:
Delphi-Quellcode:
for i := 0 to DBGrid1.Columns.Count - 1 do
begin
case DBGrid1.Columns[i].Field.DataType of
ftString, ftWideString :
begin
DBGrid1.Columns[i].Width := DBGrid1.Canvas.TextWidth(DBGrid1.Columns[i].Field.AsString);
DBGrid1.Columns[i].Alignment := taLeftJustify;
end;
ftSmallint, ftInteger, ftWord, ftAutoInc, ftLargeint :
begin
DBGrid1.Columns[i].Width := DBGrid1.Canvas.TextWidth(Format('%6.0n',[DBGrid1.Columns[i].Field.AsFloat]));
DBGrid1.Columns[i].Alignment := taRightJustify;
end;
ftFloat, ftCurrency :
begin
DBGrid1.Columns[i].Width := DBGrid1.Canvas.TextWidth(Format('%6.2n',[DBGrid1.Columns[i].Field.AsFloat]));
DBGrid1.Columns[i].Alignment := taRightJustify;
end;
ftDate :
begin
DBGrid1.Columns[i].Width := DBGrid1.Canvas.TextWidth(FormatDateTime('dd.mm:yyyy', DBGrid1.Columns[i].Field.AsDateTime]));
DBGrid1.Columns[i].Alignment := taLeftJustify;
end;
ftTime :
begin
DBGrid1.Columns[i].Width := DBGrid1.Canvas.TextWidth(FormatDateTime('hh:nn:ss', DBGrid1.Columns[i].Field.AsDateTime]));
DBGrid1.Columns[i].Alignment := taLeftJustify;
end;
ftDateTime :
begin
DBGrid1.Columns[i].Width := DBGrid1.Canvas.TextWidth(FormatDateTime('dd.mm.yyyy hh:nn:ss', DBGrid1.Columns[i].Field.AsDateTime]));
DBGrid1.Columns[i].Alignment := taLeftJustify;
end;
else
DBGrid1.Columns[i].Width := DBGrid1.Canvas.TextWidth(DBGrid1.Columns[i].Field.AsString);
DBGrid1.Columns[i].Alignment := taCenter;
end;
end;
Achso: Das ist jetzt einfach nur ungetestet hingedaddelt.
Bei den Format- bzw. FormatDateTimeaufrufen kannst Du die Formatstrings ja entsprechend Deinen Wünschen anpassen, ebenso die rechts- oder linksbündige Ausgabe ...