Solved

Cumulative totals in dbgrid

Posted on 2001-07-09
5
345 Views
Last Modified: 2010-04-06
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
Comment
Question by:z_darius
  • 3
5 Comments
 
LVL 9

Expert Comment

by:ITugay
ID: 6268323
Hi z_darius,

Ok, I will prepare one. Just a moment.

------
Igor.
0
 
LVL 9

Accepted Solution

by:
ITugay earned 200 total points
ID: 6268426
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
 
LVL 9

Expert Comment

by:ginsonic
ID: 6274595
listening
0
 
LVL 1

Author Comment

by:z_darius
ID: 6275021
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
 
LVL 9

Expert Comment

by:ITugay
ID: 6276081
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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Working with hours 3 72
How to define IfThen functions in one common unit? 4 62
Tvertscrollbox like a whatsapp layout 5 50
How to make Sign in, using Clientdataset? 1 33
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question