Datagrid column update

Creating desktop or client-server database solutions?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
geo
Posts: 31
Joined: Sun Aug 04, 2019 7:28 pm

Datagrid column update

Post by geo » Sun Oct 20, 2019 11:11 pm

Hello, I have a datagrid with 6 columns and 15 rows

I need to change the values in column 2 in all 15 rows

Is that possible by script?

Thanks

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

Re: Datagrid column update

Post by Klaus » Mon Oct 21, 2019 9:11 am

Hi geo,

yes! :-)

In this case you need to loop through the content, update your columns and "write" the content back to the datagrid:

Code: Select all

...
## dgdata = an ARRAY of the content
put the dgdata of grp "your dg here" into tArray
repeat for each key tKey in tArray
  put "your new value" into tArray[tKey]["name of your column here"]
end repeat

## Now update content of dg:
set the dgdata of grp "your dg here" to tArray
...
If your datagrid is of type TABLE then you can also do this with -> the dgtext of grp xy

Code: Select all

...
## dgTEXT = the content of your dg as TAB and CR delimited text
put the tdgtext of grp "your dg here" into tText
set itemdel to TAB
repeat with i = 1 to the num of lines of tText
  put "your new value" into item 2 of line i of tText
end repeat

## Now update content of dg:
set the dgtext of grp "your dg here" to tText
...
Best

Klaus

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9801
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Datagrid column update

Post by FourthWorld » Mon Oct 21, 2019 6:29 pm

Klaus' answer is a good one, and I have no idea if "split by column" is any faster*, but it's good to be aware of it as it can sometimes be very handy.

With this handler, pList is any tab-delimited table, such as one would get from the DataGrid's dgText; pColNumber is the number of the column you want to replace; pNewColData is a return-delimited list of values you want to replace the specified column with:

Code: Select all

function ReplaceColumn pList, pColNumber, pNewColData
   split pList by column
   put pNewColData into pList[pColNumber]
   combine pList by column
   return pList
end ReplaceColumn
* In this case I'm pretty sure this function isn't faster than Klaus' loop, since the DG's internals use an array and this function is dependent on a string. So while "split by column' may be faster than a loop for data already in string form, the overhead of converting the array to a string before passing it to the function, and then back again to put back into the DG, will probably eat a few more microseconds than a loop through the DG's own array.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

geo
Posts: 31
Joined: Sun Aug 04, 2019 7:28 pm

Re: Datagrid column update

Post by geo » Mon Oct 21, 2019 7:53 pm

Hi guys, thanks for your answers

with FourthWorlds method I get:

Type Handler: error in statement
Object Button
Line combine pList by column
Hint ReplaceColumn

With Klaus' method I can only put one new value into all lines, but I have different values for each line

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Datagrid column update

Post by bogs » Mon Oct 21, 2019 8:08 pm

Hi geo,
geo wrote:
Mon Oct 21, 2019 7:53 pm
With Klaus' method I can only put one new value into all lines, but I have different values for each line
Klaus and Richard are giving you the 'how' to do it, you have to fill in the blanks.

Just as an example (since I don't know where your 'values' are coming from, anymore than they do), you might modify this part of Klaus's example like so:

Code: Select all

## for sake of argument, lets say that you have a list of new values in a variable tmpList,
##  and each line of tmpList matches the item you want in the line in the tText...
repeat with i = 1 to the num of lines of tText
  put line i of tmpList into item 2 of line i of tText
end repeat
Image

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9801
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Datagrid column update

Post by FourthWorld » Mon Oct 21, 2019 8:10 pm

geo wrote:
Mon Oct 21, 2019 7:53 pm
Hi guys, thanks for your answers

with FourthWorlds method I get:

Type Handler: error in statement
Object Button
Line combine pList by column
Hint ReplaceColumn
The function itself is known to work, as I wrote it and tested it in LC before posting.

It will become possible to know what's causing the error when you post the code you used to call it. Offhand I'd guess you're calling it as a command instead of a function, but that's just a guess. Once I see your relevant snippet I'll have you up and running as well as my copy here.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

geo
Posts: 31
Joined: Sun Aug 04, 2019 7:28 pm

Re: Datagrid column update

Post by geo » Mon Oct 21, 2019 9:08 pm

Hello

Richard, your right, I wasn't calling it as a function, now it works

bogs, I modified it as you suggested, now it works as well

Thank you very much

Post Reply

Return to “Databases”