How to auto fit/scale DBGrid's (or other similar) columns widths according to its contents?

前端 未结 8 1890
天命终不由人
天命终不由人 2021-01-04 14:11

I am trying to make a frame with a DBGrid that will serve for more than 10 tables with half of its fields as defaults, and other fields exclusive for each table

8条回答
  •  天涯浪人
    2021-01-04 14:27

    Why to use so complicated code? :D Just use this. Its also have 5 px offset.

    procedure TForm1.FormActivate(Sender: TObject);
    
    var
    
      i:integer;
    
    begin
    
      for i :=0 to DbGrid1.Columns.Count - 1 do
       DbGrid1.Columns[i].width :=5+dbgrid1.Canvas.TextWidth(DbGrid1.Columns[i].Title.Caption);
    
    end;
    

提交回复
热议问题