Solved

Cumulative totals in dbgrid

Posted on 2001-07-09
5
348 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: 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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

732 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