如果你的序号唯一标示一行的话,我觉得用两个ADODataSet,一个用来统计,它只需要
行号和所需统计的列就可以了,并在其打开后将统计的结果放到一个可以用行号标示的
数组。另一个增加一个计算字段。注意,统计的ADODataSet,要先打开。我试了一下,
应该可以的。我用ADOConnection,ADODataSet,连接Sql Sever 的NorthWind 数据库的
[Sales Totals by Amount]表。原文件如下:
as
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls, DBCtrls, Grids, DBGrids, Db, ADODB;
type
TForm1 = class(TForm)
ADOConnection1: TADOConnection;
ADODataSet1: TADODataSet;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
DBNavigator1: TDBNavigator;
Button1: TButton;
ADODataSet1SaleAmount: TBCDField;
ADODataSet1OrderID: TAutoIncField;
ADODataSet1CompanyName: TWideStringField;
ADODataSet1ShippedDate: TDateTimeField;
ADODataSet1cl_sum: TFloatField;
ADODataSet2: TADODataSet;
procedure Button1Click(Sender: TObject);
procedure ADODataSet1CalcFields(DataSet: TDataSet);
procedure ADODataSet2AfterOpen(DataSet: TDataSet);
private
{ Private declarations }
arrSum: array of Real;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
ADODataSet2.Close;
ADODataSet2.Open;
ADODataSet1.Close;
ADODataSet1.Open;
end;
procedure TForm1.ADODataSet1CalcFields(DataSet: TDataSet);
var
i: integer;
begin
with ADODataSet1 do
begin
i := FieldValues['OrderID'];
FieldValues['cl_sum'] := arrSum
;
end;
end;
procedure TForm1.ADODataSet2AfterOpen(DataSet: TDataSet);
var
i: integer;
_Sum: Real;
begin
Setlength(arrSum,100000);
_Sum := 0;
with ADODataSet2 do
begin
First;
while not Eof do
begin
_Sum := _Sum + FieldValues['SaleAmount'];
i := FieldValues['OrderID'];
arrSum := _Sum;
Next;
end;
end;
end;
end.
:dfm
object Form1: TForm1
Left = 192
Top = 107
Width = 696
Height = 480
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object DBGrid1: TDBGrid
Left = 0
Top = 96
Width = 688
Height = 357
Align = alBottom
DataSource = DataSource1
ImeName = '中文 (简体) - 微软拼音'
TabOrder = 0
TitleFont.Charset = DEFAULT_CHARSET
TitleFont.Color = clWindowText
TitleFont.Height = -11
TitleFont.Name = 'MS Sans Serif'
TitleFont.Style = []
Columns = <
item
Expanded = False
FieldName = 'OrderID'
Visible = True
end
item
Expanded = False
FieldName = 'SaleAmount'
Visible = True
end
item
Expanded = False
FieldName = 'CompanyName'
Visible = True
end
item
Expanded = False
FieldName = 'ShippedDate'
Visible = True
end
item
Expanded = False
FieldName = 'cl_sum'
Width = 160
Visible = True
end>
end
object DBNavigator1: TDBNavigator
Left = 202
Top = 68
Width = 240
Height = 25
DataSource = DataSource1
TabOrder = 1
end
object Button1: TButton
Left = 352
Top = 32
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 2
OnClick = Button1Click
end
object ADOConnection1: TADOConnection
Connected = True
ConnectionString =
'Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security In' +
'fo=False;Initial Catalog=Northwind;Data Source=chs;Use Procedure' +
' for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ' +
'ID=CHS'
LoginPrompt = False
Provider = 'SQLOLEDB.1'
Left = 62
Top = 52
end
object ADODataSet1: TADODataSet
Connection = ADOConnection1
CursorType = ctStatic
OnCalcFields = ADODataSet1CalcFields
CommandText = 'select * from [Sales Totals by Amount]'
Parameters = <>
Left = 102
Top = 52
object ADODataSet1SaleAmount: TBCDField
FieldName = 'SaleAmount'
Precision = 19
end
object ADODataSet1OrderID: TAutoIncField
FieldName = 'OrderID'
ReadOnly = True
end
object ADODataSet1CompanyName: TWideStringField
FieldName = 'CompanyName'
Size = 40
end
object ADODataSet1ShippedDate: TDateTimeField
FieldName = 'ShippedDate'
end
object ADODataSet1cl_sum: TFloatField
FieldKind = fkCalculated
FieldName = 'cl_sum'
Calculated = True
end
end
object DataSource1: TDataSource
DataSet = ADODataSet1
Left = 154
Top = 54
end
object ADODataSet2: TADODataSet
Connection = ADOConnection1
AfterOpen = ADODataSet2AfterOpen
CommandText = 'select * from [Sales Totals by Amount]'
Parameters = <>
Left = 156
Top = 20
end
end