1

我用来计算计算字段的余额。但是当我连接dbgrid时。通过移动滚动计算是错误的。请寻求帮助

var
 Form1: TForm1;
 i : Integer;

procedure TForm1.FormShow(Sender: TObject);
 begin
 i := 0;
 DataSource1.DataSet := ADOTable1;
 DBGrid1.DataSource := DataSource1;
end;

 procedure TForm1.ADOTable1CalcFields(DataSet: TDataSet);
  begin
   i := (ADOTable1Debtor.AsInteger - ADOTable1creditor.AsInteger) + i;
   ADOTable1Total.AsInteger := i;
  end;

现在运行应用程序并移动 dbgrid 列数(总数)中的滚动条将会改变。我想知道如何停止更改。

4

2 回答 2

3

The calculated fields is designed to show value calculations at the row level and is not designed to make aggregations (calculations based on a set of rows).

For example, the database layer will fire the OnCalc event in no particular order and every time is needed to obtain the value of the field (for display pruposes, for example), since that value is not stored and may (and usually do) depend on the values of other fields.

In a set of 10 rows, you can get it called for rows 1, 2, 3, 4, 5 and then in 1 again...

You can use it, for example, in a line_total column which is the product of quantity and unit_price, but you can't use it to show e.g. the sum(line_total) of all the lines, as I infer you're trying to do in the shown code.

How to perform aggregations then?

You may want to link your DataSet to a ClientDataSet, which have AggregateFields, in which you can perform calculations like SUM(Quantity * Price) on the entire row-set or sub-sets based on a Index.

To learn more about AggregateFields read ClientDataSet Aggregates and GroupState by Cary Jensen in EDN.

于 2012-11-29T21:30:59.770 回答
0

你正在尝试的东西不起作用,因为任何滚动都会改变结果。使用 AdoDataset 代替以下命令文本

select ID, creditor, Debtor,(Select sum (Debtor-Creditor) from Table1 t where t.ID<=Table1.ID) as Total
from Table1 order by ID
于 2012-11-29T11:49:46.053 回答