如何手动执行“OnCalcFields”?事件?

发布于 2024-11-09 16:52:33 字数 533 浏览 1 评论 0原文

假设我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

嘴硬脾气大 2024-11-16 16:52:33

计算字段是在从数据库检索记录时计算的,因此对数据集调用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, an Edit followed by a Cancel call would achieve the same.

若言繁花未落 2024-11-16 16:52:33

调用刷新或关闭->可能会导致整个表从数据库重新加载。如果这不是您想要的,您可以自行调用 OnCalc 方法并将其传递给 CDS。尽管您可能需要手动滑动光标。

with DisplayAcctListCDS do begin
  First;
  while not Eof do begin
    Edit;
    DisplayAcctListCDSCalcFields(DisplayAcctListCDS);
    Next;
  end;
end;

假设 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.

with DisplayAcctListCDS do begin
  First;
  while not Eof do begin
    Edit;
    DisplayAcctListCDSCalcFields(DisplayAcctListCDS);
    Next;
  end;
end;

Assuming DisplayAcctListCDS is your TClientDataSet with calculated fields and DisplayAcctListCDSCalcFields is the generated event method for OnCalcFields.

原来分手还会想你 2024-11-16 16:52:33

这有点hack,但对我来说这个问题的答案是100%!

DBGrid.Height := 30; 
DBGrid.Height := 200; // Refresh all Rows after first
CalculatedProc(DataSet); // Refresh first calculated fields. (Write name of your calculate procedure)

This is a bit of a hack, but 100% of answer on this qestion for me!

DBGrid.Height := 30; 
DBGrid.Height := 200; // Refresh all Rows after first
CalculatedProc(DataSet); // Refresh first calculated fields. (Write name of your calculate procedure)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文