How Do I Get Aggregate Values for Columns?
This lesson will show you how to add a custom property to a data grid that returns the aggregate value of a column. Usually this would be used with data grid tables but works equally as well with data grid forms.
Add a getProp To Your Data Grid Script
The simplest way to calculate aggregate values is to add a getProp handler to your data grid script. In this example I've defined a custom property called uSumOfColumn (1). You can pass in a column name (2) and the sum of all rows of that column will be returned (3).
getprop uSumOfColumn [pColumn]
local tTotal = 0, tDataA
put the dgData of me into tDataA
repeat for each key tIndex in tDataA
add tDataA[tIndex][pColumn] to tTotal
end repeat
return tTotal
end uSumOfColumn
Using the Custom Property
Here is an example of using the custom property. When clicking on the button (1) the text of another field is set to the uSumOfColumn custom property (2).
on mouseUp pMouseButton
set the text of field "Sum" to \
the uSumOfColumn["col 1"] of group "DataGrid 1"
end mouseUp
The Result
Here the result has been put into the "Sum" field.
Ray
When you do these exercises it is useful to bear in mind that some people would be attempting this for the first time and you making assumptions will resort in frustration and being unable to get example working.
sam norris
Hello,
Could you elaborate on which assumptions you find unreasonable - or what it is the lesson lacked that would have made it less frustrating to follow?
I have a number of my own guesses (no means to copy the sample scripts springs immediately to mind) but it would be best to narrow it down in order to preserve the brevity of the lesson.