与数据集计算的字段中的DBGRID中的问题

发布于 2025-02-10 14:58:01 字数 684 浏览 1 评论 0原文

我有一个计算出的“余额”,该提交的“余额”显示了两个数据集字段之间的余额金额,我使用下一个代码来计算其值:

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 技术交流群。

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

发布评论

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

评论(1

北风几吹夏 2025-02-17 14:58:01

Handler设计用于仅依赖当前记录值的计算。

如果您使用的数据集类别允许,我建议您在加载数据后立即计算一次余额字段:

var
    Balance : Currency;
begin
    Balance := 0;

    MyQuery.DisableControls();
    try
        //loading data
        MyQuery.Open;
        
        //calculating balance
        while(not MyQuery.Eof) do
        begin
            MyQuery.Edit;
            Balance := Balance + MyQuery.FieldByName('MyFirst Field').AsCurrency - MyQuery.FieldByName('MySecond Field').AsCurrency;
            MyQuery.FieldByName('Balance').AsCurrency := Balance;
            MyQuery.Post;
            
            MyQuery.Next;
        end;
        MyQuery.First;
    finally
        MyQuery.EnableControls();
    end;
end;

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:

var
    Balance : Currency;
begin
    Balance := 0;

    MyQuery.DisableControls();
    try
        //loading data
        MyQuery.Open;
        
        //calculating balance
        while(not MyQuery.Eof) do
        begin
            MyQuery.Edit;
            Balance := Balance + MyQuery.FieldByName('MyFirst Field').AsCurrency - MyQuery.FieldByName('MySecond Field').AsCurrency;
            MyQuery.FieldByName('Balance').AsCurrency := Balance;
            MyQuery.Post;
            
            MyQuery.Next;
        end;
        MyQuery.First;
    finally
        MyQuery.EnableControls();
    end;
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文