简体   繁体   中英

How to add a column to a DBGrid by code?

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

How to do this at runtime?

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM