Einzelnen Beitrag anzeigen

wschrabi

Registriert seit: 16. Jan 2005
437 Beiträge
 
#1

DBGrid anpassen der Weite der Spalte automatisch

  Alt 23. Mai 2022, 06:17
Hallo Leute,
ich habe hier diesen Code von www.thoughtco.com/auto-fix-dbgrid-column-widths-4077417 der ja ein wenig bugg ist.
habe es vielleicht richtig ausgebessert, doch es klappt immer noch nicht. Meine Spalten werden nicht mit MAX size der DATEN pro Spalte automatisch angepasst.
Delphi-Quellcode:
procedure tform1.FixDBGridColumnsWidth(const DBGrid: TDBGrid);
var i : integer; TotWidth : integer; VarWidth : integer; ResizableColumnCount : integer; AColumn : TColumn;
begin//total width of all columns before resize
 TotWidth := 0;
 //how to divide any extra space in the grid
 VarWidth := 0;
 //how many columns need to be auto-resized
 ResizableColumnCount := 0;
 for i := 0 to -1 + DBGrid.Columns.Count do begin
 TotWidth := TotWidth + DBGrid.Columns[i].Width;
 if DBGrid.Columns[i].Field.Tag <>0 then
 Inc(ResizableColumnCount);
 end;
 //add 1px for the column separator lineif dgColLines in DBGrid.Options then
 TotWidth := TotWidth + DBGrid.Columns.Count;
 //add indicator column widthif dgIndicator in DBGrid.Options then
 TotWidth := TotWidth + IndicatorWidth;
 //width vale "left"
 VarWidth := DBGrid.ClientWidth - TotWidth;
 //Equally distribute VarWidth
 //to all auto-resizable columnsif ResizableColumnCount > 0 then
 VarWidth := varWidth div ResizableColumnCount;
 for i := 0 to -1 + DBGrid.Columns.Count do begin
 AColumn := DBGrid.Columns[i];
 if AColumn.Field.Tag <> 0 then begin
 AColumn.Width := AColumn.Width + VarWidth;
 if AColumn.Width <> 0 then
 AColumn.Width := AColumn.Field.Tag;
 end;
 end;
end; (*FixDBGridColumnsWidth*)
Weiß wer Rat wie man sowas richtig macht? Denke das Problem ist schon öfters gelöst worden. Habe Delphi Berlin.
DANKE
mfg
ws
  Mit Zitat antworten Zitat