unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, StdCtrls, Buttons, Grids, DBGrids;
type
TForm1 = class(TForm)
ADODataSet1: TADODataSet;
ADODataSet2: TADODataSet;
BitBtn1: TBitBtn;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
DBGrid2: TDBGrid;
DataSource2: TDataSource;
procedure FormCreate(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
ADODataSet1.Close;
ADODataSet1.FieldDefs.Clear;
ADODataSet1.FieldDefs.Add('type', ftstring, 20, false);
ADODataSet1.FieldDefs.Add('month', ftstring, 10, false);
ADODataSet1.FieldDefs.Add('money', ftinteger, 0, false);
ADODataSet1.CreateDataSet;
ADODataSet1.Open;
ADODataSet1.Append;
ADODataSet1.FieldByName('type').AsString := 'A1';
ADODataSet1.FieldByName('month').AsString := '1月';
ADODataSet1.FieldByName('money').AsInteger := 200;
ADODataSet1.Append;
ADODataSet1.FieldByName('type').AsString := 'A1';
ADODataSet1.FieldByName('month').AsString := '1月';
ADODataSet1.FieldByName('money').AsInteger := 201;
ADODataSet1.Append;
ADODataSet1.FieldByName('type').AsString := 'A1';
ADODataSet1.FieldByName('month').AsString := '2月';
ADODataSet1.FieldByName('money').AsInteger := 200;
ADODataSet1.Append;
ADODataSet1.FieldByName('type').AsString := 'A1';
ADODataSet1.FieldByName('month').AsString := '2月';
ADODataSet1.FieldByName('money').AsInteger := 202;
ADODataSet1.Append;
ADODataSet1.FieldByName('type').AsString := 'A1';
ADODataSet1.FieldByName('month').AsString := '3月';
ADODataSet1.FieldByName('money').AsInteger := 200;
ADODataSet1.Append;
ADODataSet1.FieldByName('type').AsString := 'A1';
ADODataSet1.FieldByName('month').AsString := '3月';
ADODataSet1.FieldByName('money').AsInteger := 203;
ADODataSet1.Append;
ADODataSet1.FieldByName('type').AsString := 'A2';
ADODataSet1.FieldByName('month').AsString := '1月';
ADODataSet1.FieldByName('money').AsInteger := 100;
ADODataSet1.Append;
ADODataSet1.FieldByName('type').AsString := 'A2';
ADODataSet1.FieldByName('month').AsString := '1月';
ADODataSet1.FieldByName('money').AsInteger := 101;
ADODataSet1.Append;
ADODataSet1.FieldByName('type').AsString := 'A2';
ADODataSet1.FieldByName('month').AsString := '2月';
ADODataSet1.FieldByName('money').AsInteger := 100;
ADODataSet1.Append;
ADODataSet1.FieldByName('type').AsString := 'A2';
ADODataSet1.FieldByName('month').AsString := '2月';
ADODataSet1.FieldByName('money').AsInteger := 102;
ADODataSet1.Append;
ADODataSet1.FieldByName('type').AsString := 'A2';
ADODataSet1.FieldByName('month').AsString := '3月';
ADODataSet1.FieldByName('money').AsInteger := 100;
ADODataSet1.Append;
ADODataSet1.FieldByName('type').AsString := 'A2';
ADODataSet1.FieldByName('month').AsString := '3月';
ADODataSet1.FieldByName('money').AsInteger := 103;
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
var
i, m, n: integer;
str, strMonth: string;
begin
ADODataSet2.Close;
ADODataSet2.FieldDefs.Clear;
ADODataSet2.FieldDefs.Add('type', ftstring, 20, false);
with ADODataSet1do
begin
first;
while not eofdo
begin
str := FieldByName('month').AsString;
if ADODataSet2.FieldDefs.IndexOf(str) < 0 then
ADODataSet2.FieldDefs.Add(str, ftinteger, 0, false);
next;
end;
ADODataSet2.CreateDataSet;
ADODataSet2.Open;
first;
while not eofdo
begin
str := FieldByName('type').AsString;
if not ADODataSet2.Locate('type', str, [loCaseInsensitive, loPartialKey]) then
begin
ADODataSet2.Append;
ADODataSet2.FieldByName('type').AsString := str;
for i := 1 to ADODataSet2.FieldCount - 1do
ADODataSet2.Fields.AsInteger := 0;
end;
next;
end;
first;
while not eofdo
begin
str := FieldByName('type').AsString;
strMonth := FieldByName('month').AsString;
m := FieldByName('money').AsInteger;
ADODataSet2.Locate('type', str, [loCaseInsensitive, loPartialKey]);
n := ADODataSet2.FieldByName(strMonth).AsInteger;
ADODataSet2.Edit;
ADODataSet2.FieldByName(strMonth).AsInteger := m + n;
next;
end;
end;
end;
end.
object Form1: TForm1
Left = 192
Top = 103
Width = 544
Height = 375
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object BitBtn1: TBitBtn
Left = 8
Top = 176
Width = 75
Height = 25
Caption = 'BitBtn1'
TabOrder = 0
OnClick = BitBtn1Click
end
object DBGrid1: TDBGrid
Left = 104
Top = 48
Width = 320
Height = 120
DataSource = DataSource1
TabOrder = 1
TitleFont.Charset = DEFAULT_CHARSET
TitleFont.Color = clWindowText
TitleFont.Height = -11
TitleFont.Name = 'MS Sans Serif'
TitleFont.Style = []
end
object DBGrid2: TDBGrid
Left = 104
Top = 224
Width = 320
Height = 120
DataSource = DataSource2
TabOrder = 2
TitleFont.Charset = DEFAULT_CHARSET
TitleFont.Color = clWindowText
TitleFont.Height = -11
TitleFont.Name = 'MS Sans Serif'
TitleFont.Style = []
end
object ADODataSet1: TADODataSet
Parameters = <>
Left = 32
Top = 16
end
object ADODataSet2: TADODataSet
Parameters = <>
Left = 32
Top = 208
end
object DataSource1: TDataSource
DataSet = ADODataSet1
Left = 136
Top = 16
end
object DataSource2: TDataSource
DataSet = ADODataSet2
Left = 136
Top = 200
end
end