我有一个主/详细 ClientDataSet 如下(这些是在运行时创建/填充的,并填充了从 API 调用返回的数据,没有数据库连接):
Services:
ID
Name
BasePrice
etc.
AddOns:
Selected
ID
ServiceID
Name
Quantity
UnitCost
TotalCost
etc.
我将服务显示为下拉字段,然后使用该服务的可用插件填充网格。'TotalCost' 字段是一个计算字段,显示在网格中它自己的列中。“已选择”字段用于跟踪网格中显示的复选框,以指示客户想要该特定附加组件。
这一切都按预期工作。我现在需要计算服务加上任何附加组件的总成本。我可以使用以下方式检索的服务成本:
ClientDataSetServices.FieldByName('BasePrice').Value
但是,我无法从每个选定的附加组件中检索 TotalCost。我以为我可以使用聚合字段,但在我的搜索中我发现使用主/详细信息设置是不可行的。我还尝试简单地遍历详细信息 ClientsDataSet,如下所示:
(within the CalcFields method of the details ClientDataSet)
// Add parts to parts cost
grdMain.DataSource.DataSet.First;
while not grdMain.DataSource.DataSet.Eof do begin
if (grdMain.DataSource.DataSet.FieldByName('Selected').Value) then begin
FPartsCost := FPartsCost +
grdMain.DataSource.DataSet.FieldByName('TotalPrice').Value;
end;
grdMain.DataSource.DataSet.Next;
end;
但这会导致无限循环。当我调试这部分代码时,我发现 ...DataSet.First 正在调用 CalcFields(或其他依次调用 CalcFields 的东西)。
How can I iterate over the DataSet of selected add-ons to calculate the total costs dynamically (whenever a selection or quantity changes)?
- 编辑 -
我尝试在详细信息表上设置聚合,如下所示:
- 添加了标签聚合'AggregatePrice'
- 设置字段如下:Active - True Name - AggregatePrice' Expression - SUM(TotalPrice) GroupingLevel - 1 IndexName - ServiceId Visible - True
当我运行它时,我收到错误消息“字段'TotalPrice'不是要在聚合中使用的正确类型的计算字段,请使用内部计算”