[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 363
  • Last Modified:

Cumulative totals in dbgrid

I have the following problem:
A a grid, fed by a ClientDataset shows hours worked.
There is one entry per record and I need to be able to show total hours as they grow with each record.

for instance

Date   Dept  Hours   Total
==========================
April    A     5         5
May      B     3         8
May      C     3        11
May      C     1        12
June     A     4        16

The data set will be subject to filter by date and other field sand I need to be able to maintain consistency in the amounts of totals.

i.e. if I filter the above to see only May records I'd like to show totals only for what's displayed like in the example below:

Date   Dept  Hours   Total
==========================
May      B     3         3
May      C     3         6
May      C     1         7

I would also like to maintain the increasing sum in the "Total" column regardles of the whic field is indexed during runtime.  So if the records for May were to be sorted in descending order the display wwould show:

Date   Dept  Hours   Total
==========================
May      C     3         3
May      C     1         4
May      B     3         7

I was playing with aggregate fields but somehow I'm missing something. I'd like this functionality to be implemented on the client side rather than through SQL.

Any ideas? Preferably a short example.
0
z_darius
Asked:
z_darius
  • 3
1 Solution
 
ITugayCommented:
Hi z_darius,

Ok, I will prepare one. Just a moment.

------
Igor.
0
 
ITugayCommented:
Hi z_darius,

here is it. This sample uses DBDemos database.

1. Drop TQuery on your form. (Query1)
2. Query1.SQL = 'select  orderno, saledate, amountpaid from orders'
3. Drop datasource, dbgrid and set it's property to show data from Query1.
4. Add all fields in Query1.FieldsEditor
5. Create new string field (for cumulative totals). "CmSum". Set it's alignment to alRightJustify.

see source code bellow.

you need to assign next events:
  Form1.OnCreate
  Form2.OnDestroy
  Query1.OnCalcFields
  Query1.AfterOpen

type
  TForm1 = class(TForm)
    Query1: TQuery;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    Query1orderno: TFloatField;
    Query1saledate: TDateTimeField;
    Query1amountpaid: TCurrencyField;
    Query1cmsum: TStringField;
    SpeedButton1: TSpeedButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Query1AfterOpen(DataSet: TDataSet);
    procedure SpeedButton1Click(Sender: TObject);
    procedure Query1CalcFields(DataSet: TDataSet);
  private
  public
    SM: TStringList;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  SM := TStringList.Create;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  SM.Free;
end;

procedure TForm1.Query1AfterOpen(DataSet: TDataSet);
var
  Sum: Real;
begin
  SM.Clear;
  Sum := 0;
  Query1.DisableControls;
  Query1.FetchAll;
  while not Query1.EOF do
  begin
    Sum := Sum + Query1.FieldByName('AmountPaid').AsFloat;
    SM.Add(FormatFloat('#,##0.#0', Sum));
    Query1.Next;
  end;
  Query1.First;
  Query1.EnableControls;
end;

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
  Query1.Open;
end;

procedure TForm1.Query1CalcFields(DataSet: TDataSet);
begin
  if SM.Count > Query1.RecNo - 1 then
    Query1.FieldByName('CmSum').AsString := SM[Query1.RecNo-1];
end;

end.

------
Igor


0
 
ginsonicCommented:
listening
0
 
z_dariusAuthor Commented:
ITugay,

The code as presented doesn't compile. Out of the box I am getting access violation error on first instance of SM.Clear. My proggie uses a number of forms and data module and I guess the order of creation screws me up. However, your code gave me a precious idea and therefore I feel you deserve the points.
Thanx.

z_darius
0
 
ITugayCommented:
Yes, it seems SM doesn't created. You can make it global variable and create/destroy  it in initialization-finalization sections.

------
Igor.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now