Solved

Cumulative totals in dbgrid

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now