Search code examples
delphifiremonkeydelphi-10-seattle

Add stringgrid column at runtime


I have found many solutions online but they aren't working becaue the StringGrid1.ColumnCount peoperty is read only. I am using Delphi 10 Seattle.

I have a StringGrid1 and I need to add columns at runtime. To be specific I have to add columns according to the size of a TList. In particular:

var a: TList<double>;
begin

 //fill the TList...

 for i := 0 to a.Count - 1 do
  begin
   StringGrid1.AddColumn(); //how can I do this?
  end;

end;

I find this very easy on Lazarus (but it has FPC of course) but on Delphi I really don't know what to do. I am working on Firemonkey.


Solution

  • Use the grid's AddObject() or InsertObject() method to add an object instance of the desired TColumn-derived class, like TStringColumn. The column object will get added to the grid's Columns array. The ColumnCount property simply returns the number of columns in the array, that is why it is read-only.