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
Solved

Cumulative totals in dbgrid

Posted on 2001-07-09
5
342 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

829 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