Page 1 of 1

Performing calculations in Data Grids

Posted: Mon Feb 17, 2014 9:49 pm
by monkey
Hello everybody,

I'm slowly getting my head around using data grids and arrays in LiveCode.

My question is this:

Once I've populated a datagrid (let's say with one column of product names and another of prices), how do I then perform calculations with the figures in the 'prices' column? I just want to do simple multiplications and divisions but first need to know how to isolate that data for manipulation.

I've been working with a lot of the online resources on Data Grids, but this doesn't seem to be covered in any of them.

Any links or pointers in the right direction would be massively appreciated.
:)

Re: Performing calculations in Data Grids

Posted: Mon Feb 17, 2014 10:28 pm
by Klaus
Hi Monkey.

1. welcome to the forum! :D
monkey wrote:I'm slowly getting my head around using data grids and arrays in LiveCode.
Congrats, that's a heavy task!

OK, one way is to use "the dgdata" of your datagrid, which is an array and "loop" through its keys.
Example to get the SUM of all columns "price":

Code: Select all

...
put the dgdata of grp "THE datagrid" into tData
put 0 into tSum
## now loop through all keys and add the columns "price"
repeat for each key tKey in tData
  add tData[tKey]["price"] to tSum
end repeat
answer "Total:" && tSum
...
You get the picture :D

Best

Klaus

Re: Performing calculations in Data Grids

Posted: Mon Feb 17, 2014 10:53 pm
by monkey
Thanks Klaus!

:)

Re: Performing calculations in Data Grids

Posted: Mon Feb 17, 2014 11:12 pm
by Zryip TheSlug
Hi Monkey,

You can download 2 example stacks for doing sum with datagrid by following this link:

http://www.aslugontheroad.com/download/category/4-lab



Best,

Re: Performing calculations in Data Grids

Posted: Tue Feb 18, 2014 10:52 am
by monkey
Thanks! :)

One last question:

When I import a file and have its contents dropped into a datagrid, I also want to display the name of the file in a label field.

Can anyone advise on how I isolate the file name and put it into the field?

Thanks again!

Re: Performing calculations in Data Grids

Posted: Tue Feb 18, 2014 1:29 pm
by Klaus
Hi Monkey,

you can check -> the dragdata["files"]
and extract the filename maybe "on dragdrop" or how your app os scripted:

Code: Select all

on dragdrop
  put the dragdata["files"] into tFile
  
  ## Pathnames are separated by SLASH /
  set itemdel to "/"

  ## Extract last item (the actiual filename) and display it
  put item -1 of tFile into fld "the filename of the dropped file"

  ## Now it is your part to fill the datagrid...
...
end dragdrop
Best

Klaus

Re: Performing calculations in Data Grids

Posted: Wed Feb 19, 2014 7:19 pm
by monkey
Brilliant! Got it all working.

Thanks again! :)