在运行时向Query添加计算字段 [英] Adding a calculated field to a Query at run time
问题描述
我知道我可以附加一个 OnCalcFields
实际进行计算的事件,但问题是在添加计算字段后,查询中没有其他字段...
我做了一些挖掘,发现所有的字段defs都被创建,但是实际的字段只能被创建
如果DefaultFields然后
CreateFields
指定默认字段
procedure TDataSet.DoInternalOpen;
begin
FDefaultFields:= FieldCount = 0;
...
end;
这将表明如果您添加字段,您只能获取添加的字段。
我希望查询中的所有字段都像我添加的那样。
这是可能的还是我必须添加我使用的所有字段?
除了计算的字段之外,还需要添加所有字段。 / p>
添加一个字段后,您必须在数据集中添加所需的所有字段。
Delphi将此持久字段称为动态字段。所有字段都是持久的或动态的。不幸的是,您不能混合使用。
另外需要注意的是,从文档中可以看到
持久性字段组件列表存储在您的应用程序中
,并且不要
更改,即使基于数据集的
数据库的结构是
更改
所以,要小心,如果您稍后在表中添加其他字段,则需要将新字段添加到组件。删除字段同样的事情。
如果你真的不想要持久字段,还有另一个解决方案。在任何应显示计算字段的网格或控件上,您可以自定义绘制。例如,许多网格控件具有OnCustomDraw事件。您可以在那里进行计算。
I'm getting data using a query in Delphi, and would like to add a calculated field to the query before it runs. The calculated field is using values in code as well as the query so I can't just calculate it in SQL.
I know I can attach an OnCalcFields
Event to actually make the calculation, but the problem is after adding the calculated field there are no other fields in the query...
I did some digging and found that all of the field defs are created but the actual fields are only created
if DefaultFields then
CreateFields
Default Fields is specified
procedure TDataSet.DoInternalOpen;
begin
FDefaultFields := FieldCount = 0;
...
end;
Which would indicate that if you add fields you only get the fields you added.
I would like all the fields in the query AS WELL AS the ones I Add.
Is this possible or do I have to add all the fields I'm using as well?
You need to add all fields in addition to your calculated field.
Once you add a field, you have to add all of the fields that you want in the data set.
Delphi calls this persistent fields versus dynamic fields. All fields are either persistent or dynamic. Unfortunately, you can't have a mixture of both.
Another thing to note, from the documentation is
Persistent fields component lists are stored in your application, and do not change even if the structure of a database underlying a dataset is changed.
So, be careful, if you later add additional fields to a table, you will need to add the new fields to the component. Same thing with deleting fields.
If you really don't want persistent fields, there is another solution. On any grid or control that should show the calculated field, you can custom draw it. For example, many grid controls have a OnCustomDraw event. You can do your calculation there.
这篇关于在运行时向Query添加计算字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!