Getting the total of the values in a column in a datagrid.

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
CAsba
Posts: 384
Joined: Fri Sep 30, 2022 12:11 pm

Getting the total of the values in a column in a datagrid.

Post by CAsba » Mon Jun 12, 2023 10:31 am

Hi,
I have a datagrid table to show the contents of an invoice. Each row - item, price, quantity, total, comprises the sales detail of a product. There may be several different products on the invoice, so several rows. My problem is, I want to add the total of the totals in the last column to present a grand total.
It occurs to me that I could do the addition, row by row, outside the dg, adding each total to an outside field then entering the field back into the DG, but one of you guys might know a more elegant way to do it, a single (simple) line of code perhaps ? Hope I've explained it properly. Looking forward to hearing from you..

Klaus
Posts: 13864
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Getting the total of the values in a column in a datagrid.

Post by Klaus » Mon Jun 12, 2023 12:49 pm

No tricks available here, you will need to make a repeat loop of some sort.


dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9738
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Getting the total of the values in a column in a datagrid.

Post by dunbarx » Tue Jun 13, 2023 4:35 pm

CAsba.
I could do the addition, row by row, outside the dg,
Just what I would do. Use the "dgText" property to get the data out.

Craig

stam
Posts: 2741
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Getting the total of the values in a column in a datagrid.

Post by stam » Wed Jun 14, 2023 6:01 pm

Just a comment - using dgText means you need to extract item x of each line. But that may causes issues down the line if you add/remove/reorder columns as that “x” may shift to a different number.

Another approach is to do the same but with the data in array form (ie dgData instead of dgText). That way you’ll always refer to the key/column by name and order is not important.
Just a thought…

S.

CAsba
Posts: 384
Joined: Fri Sep 30, 2022 12:11 pm

Re: Getting the total of the values in a column in a datagrid.

Post by CAsba » Fri Jun 16, 2023 12:02 pm

Thanks for that.

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”