Link to home
Start Free TrialLog in
Avatar of rafaelrgl
rafaelrgl

asked on

SUM COLUMN IN TDBGRID

I would like to sum one column im my tdbgrid.
Avatar of Limbeck
Limbeck

ok first of all i wouldnt do it in a grid, you will combine editable data with uneditable.

easy way is:

use a datasetprovider, connect it to your query (dataset). Add a clientdataset (CDS) and set its provider property to the datasetprovider. On the CDS, right click and choose for new field. make it an aggregate field (SumCol for a name for instance).click on Sumcol and see its properties. First, set active to true. Then go to expression and add 'sum (fieldname) '.

Close the field editor and go to the properties of the cds, make sure the property aggregatesactive=true.

Connect your datasource to the cds (in your code open/close the cds, not the query). In the dbtext set de datasource to the DS, datafield to the aggregate field.

should work.

use another  query like

select sum(FieldNameToSum) fromTableName

After posting each record into the table which is connected to the grid, reopen the query to to get the sum again


sun4sunday
To display the sum put a label of DBEdit and link to the resul of the query on it

sun4sunday
Avatar of rafaelrgl

ASKER

what happening is that:

I'm not using object tquery or connect to data base, becouse this table is on the memory, so there's no database on or object tquery.
the table in memory, is that a clientdataset?
YES, I CREATE FIELDS IN CLIENTDATASET LIKE IDUSER, IDPROD AND VALUEPROD, WHAT I DID AFTER WAS, I CLICK WITH RIGHT MOUSE BUTTON IN CREATE DATASET. AFTER THAT WAS DONE.

LET'S GO TO THE POINT, HOW CAN I SUM ONE COLUMN IN DBGRID, I GOT USE WHICH EVENT ON DBGRID AND HOW?
add an aggregate field in tghe Clientdataset as described before, not sure if you can put it in the grid or not
Hope the give soltuion will work with teh clientdataset also.

sun4sunday
First, I agree that there are more elegant solutions like the ones posted ^.   With that said, the brute-force method of summing a column in a TDBGrid would be something like:

function TAPE110.UpdateMyTableTotal : Currency;
var
  Bok : TBookmarkStr;
begin
  If not RS.Active then
    exit;
  MyColumnTotal := 0.0;
  With RS do
    if RecordCount > 0 then
      begin
        First;
        Bok := Bookmark;
        DisableControls;
        While not EOF do
          begin
            MyColumnTotal := MyColumnTotal + FieldByName('Amount').AsCurrency;
            Next;
          end;
        EnableControls;
        Bookmark := Bok;
      end;
    Result := MyColumnTotal;
end;

The above totals the value of a column called Amount that stores currency in the current dataset that the TDBGrid is pointing to.  Change this to reference your own field type and you're on your way.  

John
ASKER CERTIFIED SOLUTION
Avatar of Limbeck
Limbeck

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
right click as in go to your fieldeditor
thank's, that work perfect!!!
your welcome :)