如何手动执行“OnCalcFields”?事件?
假设我想在 TClientDataSet 上执行耗时操作期间暂时禁用 OnCalcFields
事件(例如,通过设置 cdsCalcFields := nil
)。当我重新附加 OnCalcFields
方法时,如何告诉 TClientDataSet 对计算字段执行重新计算?
另一种可能需要手动重新计算的情况是某些计算字段依赖于其他数据集的情况(例如,计算字段用于临时保存来自其他数据集的某些聚合值)。这在大多数情况下都可以正常工作,因为 OnCalcFields
事件的执行频率足以从其他数据集中获取正确的值。但在某些情况下,需要重新计算才能从其他数据集中获取正确的值。
将 AutoCalcFields
属性设置为 False
也可能会让您陷入需要手动重新计算的情况。
我已经看到了一些关于如何减少 OnCalcFields
事件执行的解释,但我找不到一种简单的方法来执行重新计算...
有什么建议吗?
Say that I temporarily want to disable the OnCalcFields
event (eg. by setting cdsCalcFields := nil
) during a time-consuming operation on a TClientDataSet. How can I tell the TClientDataSet to perform a recalculation of the calculated fields when I re-attach the OnCalcFields
method?
Another situation that might require a manual recalculation is the situation where some of the calculated fields are depending on other datasets (eg. a calculated field is used to temporarily hold some aggregated value from the other dataset). This would work fine in most cases because the OnCalcFields
events are executed often enough to get the correct value from the other dataset. But in some circumstances a recalculation is necessary to obtain the correct value from the other dataset.
Setting the AutoCalcFields
property to False
might also get you into some situation where a manual recalculation is desired.
I've seen several explanations on how to reduce the execution of the OnCalcFields
event, but I can't find a simple way to just perform a recalculation...
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
计算字段是在从数据库检索记录时计算的,因此对数据集调用
Refresh
(或关闭 -> 打开)以强制重新计算。(关于该问题的评论),要强制仅对一条记录进行重新计算,您可以在数据集上调用
RefreshRecord
。如果特定的数据集后代没有实现该方法,则调用Edit
后调用Cancel
也可以实现相同的效果。Calculated fields are calculated when records are retrieved from the database, so call
Refresh
(or Close -> Open) on the dataset to force a re-calculation.(Regarding the comments on the question), to force a re-calculation on only one record you can call
RefreshRecord
on the dataset. If the particular dataset descendant does not implement the method, anEdit
followed by aCancel
call would achieve the same.调用刷新或关闭->可能会导致整个表从数据库重新加载。如果这不是您想要的,您可以自行调用 OnCalc 方法并将其传递给 CDS。尽管您可能需要手动滑动光标。
假设 DisplayAcctListCDS 是带有计算字段的 TClientDataSet,而 DisplayAcctListCDSCalcFields 是 OnCalcFields 生成的事件方法。
Calling Refresh or Close-> can cause the entire table to reload from the database. If this isn't something you want you can just call the OnCalc method your self passing it the CDS. Though you may have to slide the cursor manually.
Assuming DisplayAcctListCDS is your TClientDataSet with calculated fields and DisplayAcctListCDSCalcFields is the generated event method for OnCalcFields.
这有点hack,但对我来说这个问题的答案是100%!
This is a bit of a hack, but 100% of answer on this qestion for me!