简体   繁体   English

如何通过代码将列添加到DBGrid?

[英]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. 使用TDBGrid ,我想添加一个新列并通过代码设置其名称。

How to do this at runtime? 如何在运行时执行此操作?

TColumn class doesn't have a Name property. TColumn类没有Name属性。 Note that it doesn't inherits from TComponent ( TColumn -> TCollectionItem -> TPersistent -> TObject ) and its parent classes don't add any Name property. 需要注意的是它不从继承TComponentTColumn - > TCollectionItem - > TPersistent - > TObject )和它的父类,不添加任何Name属性。

Anyhow, you can add a new column to a TDBGrid by simply calling the Add method of the Columns collection: 无论如何,您可以通过简单地调用Columns集合的Add方法将新列添加到TDBGrid

var
  Col : TColumn;
begin
  Col := DBGrid1.Columns.Add;
  //then you can set its properties as your needs
  Col.Title.Caption := 'MyNewColumn';
end;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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