简体   繁体   English

在 Delphi 中创建表生成器

[英]Create table generator in Delphi

I'm trying to create a table generator in delphi 10, like the one in the picture.我正在尝试在 delphi 10 中创建一个表生成器,如图中所示。 I can't find any options to edit/add buttons the "column name, column type and column size".我找不到任何选项来编辑/添加“列名、列类型和列大小”按钮。

样机屏幕截图

procedure TForm1.AddColumnClick(Sender: TObject); 
var 
  Col : TColumn; 
begin
  Col := DBGrid2.Columns.Add; 
  Col.Title.Caption := 'MyNewColumn'; 
end; 

If this is to add a column, how do I get the code for the column type and its size?如果这是添加列,如何获取列类型及其大小的代码?

Your edit which asks您的编辑要求

how do I get the code for the column type and its size?如何获取列类型及其大小的代码?

completely changes what your q is about.完全改变了你的 q 是关于什么的。 It is still far too broad, and it seems that you are intent on re-inventing the wheel, because Delphi's TDBGrid has the necessary facilities to do what you seem to want built-in.它仍然太宽泛,似乎您打算重新发明轮子,因为 Delphi 的 TDBGrid 具有必要的设施来执行您似乎想要内置的操作。 Here is my suggestion for further study:以下是我对进一步学习的建议:

  • Learn how to create persistent TFields on your dataset which will be supplying the data for the grid.了解如何在将为网格提供数据的数据集上创建持久性TField。 See http://docwiki.embarcadero.com/RADStudio/Rio/en/Persistent_Field_Components The pont of doing this is that once the dataset has persistent TFields, the design-time facilities supporting the creation of TDBGrid columns become available.请参阅http://docwiki.embarcadero.com/RADStudio/Rio/en/Persistent_Field_Components这样做的目的是,一旦数据集具有持久性 TFields,支持创建 TDBGrid 列的设计时工具就可用了。

  • Once your dataset has persistent TFields, in the IDE clear any columns the TDBGrid already has, then一旦您的数据集具有持久性 TFields,在 IDE 中清除 TDBGrid 已有的所有列,然后

  • Double-click on the DBgrid.双击 DBgrid。 You will get a pop-up columns editor with a caption like Editing DBGrid1.Columns .您将获得一个带有像Editing DBGrid1.Columns这样的标题的弹出式列编辑器。 Right click in the columns editor and select Add All Fields from the context menu.在列编辑器中右键单击并从上下文菜单中选择Add All Fields That will create the columns withthe default datatypes and sizes that Delphi uses for the grid's columns, which is what you seem to be trying to do yourself.这将使用 Delphi 用于网格列的默认数据类型和大小创建列,这似乎是您自己尝试做的。

To answer your specific question, you can get the size and datatype of a field of the dataset using要回答您的特定问题,您可以使用获取数据集字段的大小和数据类型

AField := DBGrid1.Columns[i].Field;
Size := AField.Size;
DataType := AField.DataType;

Working out how to set a suitable column width from the Size of the associated TField is left as an exercise to the reader.如何根据关联 TField 的大小设置合适的列宽作为练习留给读者。

You need to study the source code of TDBGrid to see how the grid columns adapt their behaviour to the specific TField types of the dataset.您需要研究 TDBGrid 的源代码,以了解网格列如何根据数据集的特定 TField 类型调整其行为。

To assist in that, add a button to your form and the following code为此,请在您的表单中添加一个按钮和以下代码

type
 TMyDBGrid = class(TDBGrid);

procedure TForm1.btnCreateColumnsClick(Sender: TObject);
begin
  DBGrid1.Columns.Clear;
  TMyDBGrid(DBGrid1).CreateColumns;
end;

The TMyGrid type declaration is to get access to the protected CreateColumns method of the grid. TMyGrid 类型声明是为了访问网格的受保护的CreateColumns方法。 You can then put a debuuger breakpoint on the TMyDBGrid(DBGrid1).CreateColumns call and trace into it to see what it does.然后,您可以在 TMyDBGrid(DBGrid1).CreateColumns 调用上放置一个调试器断点并跟踪它以查看它的作用。

Good luck!祝你好运!

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

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