Board index » delphi » Summing/Totalling as DBGrid Column in a Master/Details situation

Summing/Totalling as DBGrid Column in a Master/Details situation

Hi,

I would like to know of any 3rd party components for Delphi 4 (4.03)
that have the ability to sum or total a column in a DBGrid.  My problem
in the past with other components, is that they do not have the ability
to recognise the Master/Detail relationship.  i.e. when I navigate
through the records, the summing/totalling doesn't recalcualte.

Thanking you in advance.

Regards,

David Kenyon

 

Re:Summing/Totalling as DBGrid Column in a Master/Details situation


On Wed, 10 Mar 1999 12:10:44 +1030, David Kenyon <fu...@senet.com.au>
wrote:

Quote
>Hi,

>I would like to know of any 3rd party components for Delphi 4 (4.03)
>that have the ability to sum or total a column in a DBGrid.  My problem
>in the past with other components, is that they do not have the ability
>to recognise the Master/Detail relationship.  i.e. when I navigate
>through the records, the summing/totalling doesn't recalcualte.

>Thanking you in advance.

>Regards,

>David Kenyon

You can create this yourself by setting the master-detail relationship
by setting a range on the detail table in the OnDataChange event of
the master datasource instead of doing it in the Object Inspector.
When you set the range, you can then iterate through the table
deriving the totals.

Steve F (Team B)

Re:Summing/Totalling as DBGrid Column in a Master/Details situation


Hi,

Thanks for your advice Steve.  I am still learning Delphi, so could you
please provide some sample code for me to use as a base.  I understand
exactly what you are saying, I'm just having trouble implementing it.

Regards,

David Kenyon

Quote
"Steve Fischkoff (TeamB)" wrote:
> On Wed, 10 Mar 1999 12:10:44 +1030, David Kenyon <fu...@senet.com.au>
> wrote:

> >Hi,

> >I would like to know of any 3rd party components for Delphi 4 (4.03)
> >that have the ability to sum or total a column in a DBGrid.  My problem
> >in the past with other components, is that they do not have the ability
> >to recognise the Master/Detail relationship.  i.e. when I navigate
> >through the records, the summing/totalling doesn't recalcualte.

> >Thanking you in advance.

> >Regards,

> >David Kenyon

> You can create this yourself by setting the master-detail relationship
> by setting a range on the detail table in the OnDataChange event of
> the master datasource instead of doing it in the Object Inspector.
> When you set the range, you can then iterate through the table
> deriving the totals.

> Steve F (Team B)

Re:Summing/Totalling as DBGrid Column in a Master/Details situation


David, heres my interpretation,
remember that TotalSum has to be a global variable

procedure TForm1.OnDataChange(Sender: TObject);
var
    RangeVariable : Integer;
begin
    TotalSum := 0;
    RangeVariable := Table1.FieldByName('SomeField').AsInteger;
    Table2.SetRange([RangeVariable], [RangeVariable]);
    try
        Table2.First;
        while not Table2.eof do begin
            TotalSum := TotalSum +
Table2.FieldByName('SomeOtherField').AsFloat;
            Table2.Next;
        end;
    finally
        Table2.CancelRange;
    end;
end;

/Marcus

Other Threads