Page 1 of 1

Calculate grand total for line prices in a data grid

Posted: Sun Jan 05, 2014 11:53 pm
by jalz
Hi Guys,
I have a datagrid, which I have as a form. Each row contains two fields, description and order_price. What I would like to do is sum the order_price for each row every time it is changed and place it in a field called total_price.

I'm guessing I need to execute the script onTextChanged which is fine. What I can't seem to figure out is how to do the actual calculation, i.e. retrieve the data from the datagrid. I found this tutorial http://lessons.runrev.com/s/lessons/m/d ... or-columns, but its mainly for table based data grid, does anyone have any form related tutorial/samples?

Many thanks

Jalz

Re: Calculate grand total for line prices in a data grid

Posted: Mon Jan 06, 2014 12:35 am
by dunbarx
Hi.

Check out the dataGrid guide. You will want to look at the "dgText" or the "dgData". Either of these will retrieve the contents of the data grid, the first in the clear and the second in an array. Once you have that, you are home free. For example, if you just get the dgText, this will be read as a tab and return delimited dataset, and you can extract the items desired in each line and do the math.

If I read your post right, you want to sum an entire column of data on each change? In this case, you have to crunch the data. There are built-in properties that will return the data in a row (dgDataOfLine), but I do not think (could be wrong) there is one that will do so for a column. I think this is just because that is how datasets are organized. See p. 94 of the guide...

Craig Newman

Re: Calculate grand total for line prices in a data grid

Posted: Mon Jan 06, 2014 1:36 am
by Klaus
Hi Jalz,

I would rather use a "closefield" handler to call your calculating routine.
Calculate like this, maybe put it into the card script:

Code: Select all

function get_order_price
  put the dgData of grp "your datagrid here" into tData
  put 0 into tTotal
  repeat for each key tkey in tData
    add tData[tKey]["order_price"] to tTotal
  end repeat
  return tTotal
end get_order_price
...and call AFTER the datagrid has been updated:

Code: Select all

...
put get_order_price() into fld "total_price"
...
Best

Klaus

Re: Calculate grand total for line prices in a data grid

Posted: Tue Jan 07, 2014 2:00 pm
by jalz
Hi Klaus,

Thank you for the code snippet. I've created a simple sample file, but I cant seem to get the value of order_price from the data grid (bear in mind I am using a form). Any ideas what I'm missing?

Many thanks
Jalz

Re: Calculate grand total for line prices in a data grid

Posted: Tue Jan 07, 2014 2:10 pm
by Klaus
Hi jalz,

your problem is that you have EDITABLE fields in your datagrid and
no script to add the entered data to the datagrid!

So getting "the dagdata..." will always be empty and this "total_price"!
And you did not prepare e.g. the "FillInData" handler in the "row behavior"
to fill your datagrid correctly!


Best

Klaus