[SOLVED] DBGrid autosize columns lazarus / 31-LCL ---------------------------------------------------------------------- BigChimp · 2012-03-09 11:03 Hi all, Lazarus 0.9.31 (6 March), Windows. I'm working on a GUI for my firewall log reader https://bitbucket.org/reiniero/smalltools/src, directory iptableslog. For this, I use a dbgrid to show an imported csv file. However, in its default settings, the columns appear very wide. .AutoSizeColumns has no effect. I can use .AutoFillColumns, but then the columns become very narrow and text is chopped off. See example screenshots Any tips on getting the columns right? If need be, I could define them designtime... but I'd like to be flexible as the format of the log files may change... Additionally, the sdfdataset may read the columns incorrectly, resulting in wide columns; haven't checked that... Thanks, BigChimp ---------------------------------------------------------------------- jesusr · 2012-03-09 20:29 Can you please sumit a bug report with a sample, I will try to fix it. ---------------------------------------------------------------------- BigChimp · 2012-03-10 10:30 Thanks Jesus, will do that... ---------------------------------------------------------------------- BigChimp · 2012-03-11 07:01 Jesus, thanks for your offer, I think you already fixed my problem See http://bugs.freepascal.org/view.php?id=14006 I've uploaded my test program there & I think the issue can be closed but.... can't dgAutoSizeColumns be on by default? In any case, I've updated the grids wiki with this info: http://wiki.lazarus.freepascal.org/Grids_Reference_Page#TCustomDBGrid Thanks for the fix! ---------------------------------------------------------------------- kabalas · 2015-06-09 00:13 Hi I found the following to work and run the procedure after I open the query. procedure SetGridColumnWidths(Grid: TDBGrid); const   DEFBORDER = 10; var   temp, n: Integer;   lmax: array [0..30] of Integer; begin   with Grid do   begin     Canvas.Font := Font;     for n := 0 to Columns.Count - 1 do       //if columns[n].visible then       lmax[n] := Canvas.TextWidth(Grid.DataSource.DataSet.Fields[n].DisplayLabel) + DEFBORDER;     grid.DataSource.DataSet.First;     while not grid.DataSource.DataSet.EOF do     begin       for n := 0 to Columns.Count - 1 do       begin         //if columns[n].visible then begin         temp := Canvas.TextWidth(trim(Columns[n].Field.DisplayText)) + DEFBORDER;         if temp > lmax[n] then lmax[n] := temp;         //end; { if }       end; {for}       grid.DataSource.DataSet.Next;     end; { while }     grid.DataSource.DataSet.First;     for n := 0 to Columns.Count - 1 do       if lmax[n] > 0 then         Columns[n].Width := lmax[n];   end; { With } end; {SetGridColumnWidths  } ---------------------------------------------------------------------- jufau · 2015-07-02 14:05 Sorry guys!!! I did not read the whole article. So I haven't noticed that the solution had been given already. Anyways, use this as shortcut if needed so. It seems that the problem has already been SOLVED. But just another solution which works just perfect for me. - Select the DBGrid; - On its properties, click and expand Options, then set dgAutoSizeColumns to TRUE. That's all. No coding needed as you can see. I am using Lazarus IDE 1.4.0 Hope it helped somehow.