与数据集计算的字段中的DBGRID中的问题
我有一个计算出的“余额”,该提交的“余额”显示了两个数据集字段之间的余额金额,我使用下一个代码来计算其值:
var
Balance : Currency;
...
procedure TMyForm.MyQueryCalcFields(DataSet: TDataSet);
begin
if MyQuery.FieldByName('MyFirst Field').AsCurrency > 0 then
Balance := Balance+MyQuery.FieldByName('MyFirst Field').AsCurrency;
if MyQuery.FieldByName('MySecond Field').AsCurrency > 0 then
Balance := Balance-MyQuery.FieldByName('MySecond Field').AsCurrency;
MyQuery.FieldByName('Balance').AsCurrency := Balance;
end;
并且我使用tdbgrid
显示了我的查询结果DBGrid Display的结果正确的值直到我开始缩放其垂直滚动条为止,在这种情况下,它显示错误的值。
我尝试将“ 0”分配给余额,但相同(开始滚动时值错误的值)。
如何使DBGrid保持正确的值?
I have a calculated filed 'Balance' which display the balance amount between two dataset fields and I used the next code to calculate its value:
var
Balance : Currency;
...
procedure TMyForm.MyQueryCalcFields(DataSet: TDataSet);
begin
if MyQuery.FieldByName('MyFirst Field').AsCurrency > 0 then
Balance := Balance+MyQuery.FieldByName('MyFirst Field').AsCurrency;
if MyQuery.FieldByName('MySecond Field').AsCurrency > 0 then
Balance := Balance-MyQuery.FieldByName('MySecond Field').AsCurrency;
MyQuery.FieldByName('Balance').AsCurrency := Balance;
end;
and I used TDBGrid
to display the result of my query , The dbgrid display the correct values until I start srolling its vertical scrollbars, in this case it shows wrong values.
I tried assigning '0' to the balance but its the same (Wrong values when start scrolling).
How I can make the dbgrid keep showing the right values?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Handler设计用于仅依赖当前记录值的计算。
如果您使用的数据集类别允许,我建议您在加载数据后立即计算一次余额字段:
OnCalcField
event handler is designed for calculations that rely on the current record values only.If the dataset's class you're using allows that, I suggest you to calculate the balance field once, as soon as you load the data, for example: