so für alle die des auch suchen...
so kann man en autofit für alle spalten auf sein dbgrid machen:
Delphi-Quellcode:
procedure TForm1.Button4Click(Sender: TObject);
var
iMaxTextLength : Integer;
i, j : Integer;
begin
//Columns
For i := 0 To dbgrid_layout.Columns.Count -1 Do
Begin
//Row per Column
iMaxTextLength := 0;
For j := 1 To dbgrid_layout.DataSource.DataSet.RecordCount Do
Begin
dbgrid_layout.DataSource.DataSet.RecNo := j;
If dbgrid_layout.Canvas.TextWidth(dbgrid_layout.Columns[i].Field.DisplayText) > iMaxTextLength Then
Begin
iMaxTextLength := dbgrid_layout.Canvas.TextWidth(dbgrid_layout.Columns[i].Field.DisplayText);
End;
End;
//Check Title Text longer then Item Text
If dbgrid_layout.Canvas.TextWidth(dbgrid_layout.Columns[i].Title.Caption) > iMaxTextLength Then
Begin
dbgrid_layout.Columns[i].Width := dbgrid_layout.Canvas.TextWidth(dbgrid_layout.Columns[i].Title.Caption) + 10;
End Else
Begin
dbgrid_layout.Columns[i].Width := iMaxTextLength + 10;
End;
End;
dbgrid_layout.Repaint;
end;
ob des die beste lösung is... kp... aufjedenfall funktionierts
Edit: Source Code aktualisiert