Marco Web Center

[an error occurred while processing this directive]

Home: Code Repository: Mastering Delphi 5

Project CDSCALCS

Project Structure


CDSCALCS.DPR

program CdsCalcs;

uses
  Forms,
  CalcForm in 'CalcForm.pas' {Form1};

{$R *.RES}

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

CALCFORM.PAS

unit CalcForm;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Db, Grids, DBGrids, DBClient, DBTables, StdCtrls, Mask, DBCtrls, ExtCtrls,
  Provider;

type
  TForm1 = class(TForm)
    Table1: TTable;
    ClientDataSet1: TClientDataSet;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    ClientDataSet1Name: TStringField;
    ClientDataSet1Capital: TStringField;
    ClientDataSet1Continent: TStringField;
    ClientDataSet1Area: TFloatField;
    ClientDataSet1Population: TFloatField;
    ClientDataSet1TotalArea: TAggregateField;
    Panel1: TPanel;
    Label2: TLabel;
    DBEdit1: TDBEdit;
    Button1: TButton;
    Label1: TLabel;
    DataSetProvider1: TDataSetProvider;
    procedure Button1Click(Sender: TObject);
    procedure ClientDataSet1ContinentGetText(Sender: TField;
      var Text: String; DisplayText: Boolean);
    procedure FormCreate(Sender: TObject);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
  Label1.Caption :=
    'Area: ' + ClientDataSet1TotalArea.DisplayText +
    #13'Population : ' + FormatFloat ('###,###,###', ClientDataSet1.Aggregates [1].Value) +
    #13'Number : ' + IntToStr (ClientDataSet1.Aggregates [0].Value);
end;

procedure TForm1.ClientDataSet1ContinentGetText(Sender: TField;
  var Text: String; DisplayText: Boolean);
begin
  if gbFirst in ClientDataSet1.GetGroupState (1) then
    Text := Sender.AsString
  else
    Text := '';
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  ClientDataSet1.Active := True;
end;

end.

CALCFORM.DFM

object Form1: TForm1
  Left = 192
  Top = 107
  Width = 573
  Height = 421
  Caption = 'CdsCalcs'
  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 DBGrid1: TDBGrid
    Left = 0
    Top = 0
    Width = 565
    Height = 336
    Align = alClient
    DataSource = DataSource1
    TabOrder = 0
    TitleFont.Charset = DEFAULT_CHARSET
    TitleFont.Color = clWindowText
    TitleFont.Height = -11
    TitleFont.Name = 'MS Sans Serif'
    TitleFont.Style = []
    Columns = <
      item
        Expanded = False
        FieldName = 'Continent'
        Visible = True
      end
      item
        Expanded = False
        FieldName = 'Name'
        Visible = True
      end
      item
        Expanded = False
        FieldName = 'Capital'
        Visible = True
      end
      item
        Expanded = False
        FieldName = 'Area'
        Visible = True
      end
      item
        Expanded = False
        FieldName = 'Population'
        Visible = True
      end>
  end
  object Panel1: TPanel
    Left = 0
    Top = 336
    Width = 565
    Height = 58
    Align = alBottom
    TabOrder = 1
    object Label2: TLabel
      Left = 24
      Top = 23
      Width = 97
      Height = 13
      Caption = 'TotalArea aggregate'
    end
    object Label1: TLabel
      Left = 424
      Top = 13
      Width = 32
      Height = 13
      Caption = 'Label1'
    end
    object DBEdit1: TDBEdit
      Left = 144
      Top = 19
      Width = 121
      Height = 21
      DataField = 'TotalArea'
      DataSource = DataSource1
      ReadOnly = True
      TabOrder = 0
    end
    object Button1: TButton
      Left = 304
      Top = 17
      Width = 105
      Height = 25
      Caption = 'Get Aggregates'
      TabOrder = 1
      OnClick = Button1Click
    end
  end
  object Table1: TTable
    Active = True
    DatabaseName = 'DBDEMOS'
    TableName = 'COUNTRY.DB'
    Left = 24
    Top = 24
  end
  object ClientDataSet1: TClientDataSet
    Aggregates = <
      item
        Active = True
        AggregateName = 'Count'
        Expression = 'COUNT (NAME)'
        GroupingLevel = 1
        IndexName = 'ClientDataSet1Index1'
        Visible = False
      end
      item
        Active = True
        AggregateName = 'TotalPopulation'
        Expression = 'SUM (POPULATION)'
        Visible = False
      end>
    AggregatesActive = True
    FieldDefs = <
      item
        Name = 'Name'
        DataType = ftString
        Size = 24
      end
      item
        Name = 'Capital'
        DataType = ftString
        Size = 24
      end
      item
        Name = 'Continent'
        DataType = ftString
        Size = 24
      end
      item
        Name = 'Area'
        DataType = ftFloat
      end
      item
        Name = 'Population'
        DataType = ftFloat
      end>
    IndexDefs = <
      item
        Name = 'ClientDataSet1Index1'
        Fields = 'Continent'
        GroupingLevel = 1
      end>
    IndexName = 'ClientDataSet1Index1'
    Params = <>
    ProviderName = 'DataSetProvider1'
    StoreDefs = True
    Left = 24
    Top = 120
    object ClientDataSet1Continent: TStringField
      FieldName = 'Continent'
      OnGetText = ClientDataSet1ContinentGetText
      Size = 24
    end
    object ClientDataSet1Name: TStringField
      FieldName = 'Name'
      Size = 24
    end
    object ClientDataSet1Capital: TStringField
      FieldName = 'Capital'
      Size = 24
    end
    object ClientDataSet1Area: TFloatField
      FieldName = 'Area'
    end
    object ClientDataSet1Population: TFloatField
      FieldName = 'Population'
    end
    object ClientDataSet1TotalArea: TAggregateField
      FieldName = 'TotalArea'
      ReadOnly = True
      Visible = True
      Active = True
      DisplayFormat = '###,###,###'
      Expression = 'SUM(AREA)'
      GroupingLevel = 1
      IndexName = 'ClientDataSet1Index1'
    end
  end
  object DataSource1: TDataSource
    DataSet = ClientDataSet1
    Left = 24
    Top = 176
  end
  object DataSetProvider1: TDataSetProvider
    DataSet = Table1
    Constraints = True
    Left = 24
    Top = 72
  end
end