Column Autosize in TStringGrid? lazarus / 31-LCL ---------------------------------------------------------------------- egsuh · 2023-08-14 03:22 Recently I installed Lazarus 2.2.6. Are there any way that I can set auto column size in TStringGrid? I think I did this in the past, but cannot find the method any more. ---------------------------------------------------------------------- Handoko · 2023-08-14 03:52 I just tested TStringGrid.AutoSizeColumn(aCol) on my Lazarus 2.2.6 Linux, it worked: https://lazarus-ccr.sourceforge.io/docs/lcl/grids/tcustomstringgrid.au tosizecolumn.html ---------------------------------------------------------------------- egsuh · 2023-08-14 05:03 Thank you Handoko. It means I should run the program whenever I change something, doesn't it? ---------------------------------------------------------------------- rvk · 2023-08-14 05:11 TCustomGrid.AutoFillColumns then? https://lazarus- ccr.sourceforge.io/docs/lcl/grids/tcustomgrid.autofillcolumns.html That property should do it automatically after changes. ---------------------------------------------------------------------- Handoko · 2023-08-14 05:24 I just tested AutoFillColumns, it behaved very similar to AutoSizeColumn: - AutoFillColumns will resize the width of all columns - AutoSizeColumn will only resize the width of the column supplied in the parameter - Both AutoFillColumns and AutoSizeColumn won't autoresize if the data changed If the data of the related column has been changed, we need to call the function again, they won't automatically resize it for you. Tested only on Linux, not sure how will they behave on other OSes. ---------------------------------------------------------------------- rvk · 2023-08-14 05:31 Quote from: Handoko on August 14, 2023, 07:24:19 am- Both AutoFillColumns and AutoSizeColumn won't autoresize if the data changed Ha, that's strange and not according to documentation. AutoFillColumns should do that. QuoteNote that TGridColumn instances in Columns are initially created with SizePriority set to 1. For TDbGrid, which automatically populates Columns, this means the user will not be able to resize these columns using the mouse (since they are auto-sized). AutoFillColumns is a property to set once and not a function to call every time. You can't set it to true twice (you can but that shouldn't have any effect). ---------------------------------------------------------------------- Handoko · 2023-08-14 05:41 I retested AutoFillColumns again using this code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Grids, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)  Button1: TButton;    StringGrid1: TStringGrid;    procedure Button1Click(Sender: TObject);    procedure FormCreate(Sender: TObject);  private  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject);begin  StringGrid1.Options := StringGrid1.Options + [goEditing];end; procedure TForm1.Button1Click(Sender: TObject);begin  StringGrid1.AutoFillColumns := True;end; end. It seemed buggy on my test, or I just don't understand how it works? See the attached screenshot below, the columns weren't resize properly. ---------------------------------------------------------------------- rvk · 2023-08-14 06:48 Quote from: Handoko on August 14, 2023, 07:41:40 amI retested AutoFillColumns again using this code:Woops, sorry, I was wrong  AutoFillColumns is for autofilling the columns to the size of the grid so there is no whitespace next to the last column. It will also hold this setting when the grid is resized (because of a right anker). So calling StringGrid1.AutoSizeColumns after each change is the only way. But I don't see an OnChange event. ---------------------------------------------------------------------- egsuh · 2023-08-14 09:30 AutoFillColumns divide column sizes evenly to all columns. No? With TDBGrid, I could set both column autosize and autofillcolumns by setting size priorityhy of the last column to 2. ---------------------------------------------------------------------- Zvoni · 2023-08-14 12:10 Quote from: rvk on August 14, 2023, 08:48:08 amBut I don't see an OnChange event. OnEditingDone? ---------------------------------------------------------------------- rvk · 2023-08-14 12:39 Quote from: Zvoni on August 14, 2023, 02:10:27 pmQuote from: rvk on August 14, 2023, 08:48:08 amBut I don't see an OnChange event. OnEditingDone? I meant something that would also trigger if you change cells in code. You could use some idle event but that would use a lot of resources. I guess it should also just be done in code then. ---------------------------------------------------------------------- Zvoni · 2023-08-14 13:09 Quote from: rvk on August 14, 2023, 02:39:35 pmQuote from: Zvoni on August 14, 2023, 02:10:27 pmQuote from: rvk on August 14, 2023, 08:48:08 amBut I don't see an OnChange event. OnEditingDone? I meant something that would also trigger if you change cells in code. In that case you ARE already within "running" code. Fire off an AutoSizeColumn, and you're good. OnEditingDone was more for if you change something in the Grid itself The only other Event catching my eye is OnDrawCell or OnPaint