1

Using a TDBGrid, I want to add a new column and set its name by code.

How to do this at runtime?

Fabrizio
  • 7,603
  • 6
  • 44
  • 104
Johnny
  • 57
  • 3
  • 8

1 Answers1

4

TColumn class doesn't have a Name property. Note that it doesn't inherits from TComponent (TColumn -> TCollectionItem -> TPersistent -> TObject) and its parent classes don't add any Name property.

Anyhow, you can add a new column to a TDBGrid by simply calling the Add method of the Columns collection:

var
  Col : TColumn;
begin
  Col := DBGrid1.Columns.Add;
  //then you can set its properties as your needs
  Col.Title.Caption := 'MyNewColumn';
end;
Fabrizio
  • 7,603
  • 6
  • 44
  • 104
  • @Johnny: You're welcome, please [accept the answer](https://meta.stackexchange.com/a/5235) if it solved the problem – Fabrizio May 03 '17 at 13:09
  • If I create a column in a RxDBGrid after dataset is loaded, it shows the new column, but all other columns disappear. Do you maybe know why? I need both columns dynamically loaded from dataset and new column dynamically created through code.... – zlatko Feb 26 '23 at 15:05