format datagrid values with function

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
jalz
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 340
Joined: Fri Sep 12, 2008 11:04 pm

format datagrid values with function

Post by jalz » Tue May 05, 2015 10:50 pm

Hi Guys,

I have the following sql statement below which works OK. However I've written a function called returnCurrency which adds currency symbol, decimal, commas for every thousand, two digits to indicate pence etc to a whole number and I want to wrap this function around my fields item_price and line_price. As I'm populating a datagrid using "set the dgText", I'm not quite sure how to achieve this - can someone give me some pointers. My initial thoughts were to convert the item values before I use the set dgText (as you can see, ive commented them out as they were not working), but that didn't work as it populated the column with data formatted incorrectly, perhaps it was the comma that threw out the formatting...

Code: Select all

put "SELECT material, item_price, qty, line_price, lineID FROM project_material WHERE projectID =" && "'" & tProjectID & "'" & "ORDER BY lineID" into tTheSQLQuery
      
      put revDataFromQuery(tab, cr, gConnID, tTheSQLQuery) into tTheData
      if tTheData begins with "revdberr," then
         delete item 1 of tTheData
      else
         --put returnCurrency(item 2 of tTheData) into item 2 of tTheData
         --put returnCurrency(item 4 of tTheData) into item 4 of tTheData
         
         set the dgText[true] of me to "material" & tab & "item_price" & tab & "qty" & tab & "line_price" & tab & "lineID" & cr & tTheData
      end if
Last edited by jalz on Wed May 06, 2015 12:05 pm, edited 2 times in total.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10394
Joined: Wed May 06, 2009 2:28 pm

Re: format datagrid values with function

Post by dunbarx » Tue May 05, 2015 11:23 pm

Hi.

If you are working the data in the clear, before reloading the DG, then simply apply your function to each item (delimited as you have them) as appropriate, no? This is what I do, because I am still uncomfortable working inside the DG itself, mucking around with the internal "cell" values using native DG gadgetry. I do it all outside in a normal variable, and unload and reload ad nauseam.

Craig Newman

sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm

Re: format datagrid values with function

Post by sritcp » Wed May 06, 2015 7:25 pm

jalz wrote:.....My initial thoughts were to convert the item values before I use the set dgText (as you can see, ive commented them out as they were not working), but that didn't work as it populated the column with data formatted incorrectly, perhaps it was the comma that threw out the formatting...
Did you
set the itemDel to tab
before doing the conversion?

Regards,
Sri

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10394
Joined: Wed May 06, 2009 2:28 pm

Re: format datagrid values with function

Post by dunbarx » Wed May 06, 2015 7:54 pm

Hi.

Sri wrote:
Did you set the itemDel to tab before doing the conversion?
Is that possibly your issue?

I had written:
...apply your function to each item (delimited as you have them)...
Do you know what we mean?

Craig

jalz
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 340
Joined: Fri Sep 12, 2008 11:04 pm

Re: format datagrid values with function

Post by jalz » Wed May 06, 2015 10:25 pm

Thanks to both of you, yes it was the item delimiter that I forgot to set….

Now that its set, the first line of data is formatting correctly, but subsequent lines are not. So I'm trying to use an repeat each line loop, but its not returning my results. Am I putting the data back correctly in the line the way I have written it?

Code: Select all


      set itemdel to TAB
      
      if tTheData begins with "revdberr," then
         delete item 1 of tTheData
      else
         repeat for each line i in tTheData
            put returnCurrency(item 2 of i, "true") into item 2 of i
            put returnCurrency(item 4 of i, "true") into item 4 of i
         end repeat
        
         set the dgText[true] of me to "material" & tab & "item_price" & tab & "qty" & tab & "line_price" & tab & "lineID" & cr & tTheData

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10394
Joined: Wed May 06, 2009 2:28 pm

Re: format datagrid values with function

Post by dunbarx » Wed May 06, 2015 11:44 pm

Hi.

You are changing the item values, line by line, of the "local" contents of tTheData WITHIN the loop, but never actually doing anything with those changes. Step through the repeat loop and watch what happens. The code executes just fine, but nothing comes of it. Each new line is modified in turn, and then, nothing.

There are about a million ways out of this. I bet you do not need one from me.

Craig

Post Reply