Performing calculations in Data Grids

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
monkey
Posts: 9
Joined: Mon Feb 17, 2014 9:38 pm

Performing calculations in Data Grids

Post by monkey » Mon Feb 17, 2014 9:49 pm

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.
:)

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Performing calculations in Data Grids

Post by Klaus » Mon Feb 17, 2014 10:28 pm

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

monkey
Posts: 9
Joined: Mon Feb 17, 2014 9:38 pm

Re: Performing calculations in Data Grids

Post by monkey » Mon Feb 17, 2014 10:53 pm

Thanks Klaus!

:)

Zryip TheSlug
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 163
Joined: Tue Jan 26, 2010 10:15 pm
Contact:

Re: Performing calculations in Data Grids

Post by Zryip TheSlug » Mon Feb 17, 2014 11:12 pm

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,
TheSlug
http://www.aslugontheroad.com - Tutorials, demo stacks and plugins for LiveCode
Data Grid Helper - An intuitive interface for building LiveCode's Data Grids
Excel Library- Extends the LiveCode language for controlling MS Excel

monkey
Posts: 9
Joined: Mon Feb 17, 2014 9:38 pm

Re: Performing calculations in Data Grids

Post by monkey » Tue Feb 18, 2014 10:52 am

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!

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Performing calculations in Data Grids

Post by Klaus » Tue Feb 18, 2014 1:29 pm

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

monkey
Posts: 9
Joined: Mon Feb 17, 2014 9:38 pm

Re: Performing calculations in Data Grids

Post by monkey » Wed Feb 19, 2014 7:19 pm

Brilliant! Got it all working.

Thanks again! :)

Post Reply