Board index » delphi » How can I sum a DBGrid column

How can I sum a DBGrid column

I am displaying records of a detail table from a one to many link in a
DBGrid.  I wish to sum the fields in one column of the DBGrid but have not
been able to figure how I should do this.  Can anyone help?

 

Re:How can I sum a DBGrid column


Try a TQuery to do the calculation and put a TDBEdit componenent on the form
attached to the query to display the sum.

Stu Rich

Quote
dickM wrote in message <01bcdb8f$89678380$21b173cf@dickythinkpad>...
>I am displaying records of a detail table from a one to many link in a
>DBGrid.  I wish to sum the fields in one column of the DBGrid but have not
>been able to figure how I should do this.  Can anyone help?

Re:How can I sum a DBGrid column


Quote
> I am displaying records of a detail table from a one to many link in a
> DBGrid.  I wish to sum the fields in one column of the DBGrid but have
not
> been able to figure how I should do this.  Can anyone help?

Try using TQuery component, write SQL statment starting with

SELECT SUM(field name you want to sum)
FORM name of table you want to sum
WHERE condition simillar to join in you master - detail table

To do this Query dynamic (that means - sensitive for data change) set
TQuery.RequestLive
property to true.

I'm not quite sure for this solution, but it is more or less the way I did
it.

In Delphi we trust!

Piotr Stok3osa
pi...@ata1.ata.com.pl

Re:How can I sum a DBGrid column


On 18 Oct 1997 04:33:34 GMT, "dickM" <di...@bentonrea.com> wrote:

Quote
>I am displaying records of a detail table from a one to many link in a
>DBGrid.  I wish to sum the fields in one column of the DBGrid but have not
>been able to figure how I should do this.  Can anyone help?

You can use SQL to calculate sum and link this to a DBEdit via
datasource. Your TQuery's datasource is the master table's data source
and a typical SQL statement is:
SELECT DISTINCT ":MyTempDir:IMTMP01.db"."Drawing number",
":MyTempDir:IMTMP01.db"."Drawing sheet", SUM(Quantity) AS Quantity,
SUM(":MyTempDir:IMTMP01.db"."Supply cost") AS
":MyTempDir:IMTMP01.db"."Supply cost",
SUM(":MyTempDir:IMTMP01.db"."{*word*114} cost") AS
":MyTempDir:IMTMP01.db"."{*word*114} cost"
FROM ":MyTempDir:IMTMP01.db"
WHERE
(":MyTempDir:IMTMP01.db"."Drawing number" =:"Drawing number")
 AND (":MyTempDir:IMTMP01.db"."Drawing sheet" =:"Drawing Sheet")
GROUP BY ":MyTempDir:IMTMP01.db"."Drawing number",
":MyTempDir:IMTMP01.db"."Drawing sheet"
ORDER BY ":MyTempDir:IMTMP01.db"."Drawing number",
":MyTempDir:IMTMP01.db"."Drawing sheet"

Note lines with "=:". Those are the linking fields between the master
and detail. (IMTMP01.db is the detail table). Drawing number and
Drawing sheet become "Params" (see object inspector for TQuery) and
you have to set their type correctly. In above case they are string
type.

Jouko Tolonen
South Africa
jo...@megaweb.co.za

Other Threads