Datagrids -- make my head hurt

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
altamative131
Posts: 7
Joined: Mon Jan 13, 2014 6:57 pm

Datagrids -- make my head hurt

Post by altamative131 » Thu Jan 23, 2014 5:57 pm

I am trying to take text from fields in my application, and have them write into a Datagrid.

For the life of me I do not know (and haven't found yet) how to set text to go into the next line of a datagrid.

on mouseUp
get the dgtext of group "datagrid 1"
set the itemdel to tab
put the text of field "Name" into item 4 of it
set the dgtext of group "datagrid 1" to it
end mouseUp

I would like for every time the I submit something that it write the data into the next field of the datagrid. I can manually tell it which line to write it into -- but I don't want to overwrite text that is already there.

Any help would be much appreciated.

Thanks

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

Re: Datagrids -- make my head hurt

Post by Klaus » Thu Jan 23, 2014 6:09 pm

Hi altamative131,
Datagrids -- make my head hurt
welcome to the club! 8)

Datagrids are the most complex beasts we have in Livecode and you will need patience
and alot of practise to not master but simply use them more or less efficient!

Go here and download the complete datagrid PDF, which are the only docs for datagrids:
http://lessons.runrev.com/m/datagrid
Read this a couple of times and then again, that's what I do befoe every new project with datagrids involved :D

I am still not sure what you wnat to do?
Add new line to an existing datagrid?

Some general hints:
the dgtext of grp xyz will return ALL the data (all lines and all columns) of the datagrid!
So putting something into ITEM 4 WITHOUT defining a line will overwrite everything after the 4th item of line 1
when you set teh dgtext back into the datagrid!


Best

Klaus

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

Re: Datagrids -- make my head hurt

Post by dunbarx » Thu Jan 23, 2014 7:35 pm

Hi.

What Klaus said.

Do this experiment. In a new card, make two buttons and a dataGrid. In the script of btn 1:

Code: Select all

on mouseUp
   set the dgText of grp 1 to "A" & tab & "B" & tab & "C" & return & "D" & tab\
    & "E" & tab & "F" & return & "G" & tab & "H" & tab & "J"
end mouseUp
In btn 2:

Code: Select all

on mouseup
   get the dgText of grp 1
   set the itemdel to tab
   put "XXX" into item 2 of it --WE WILL CHANGE THE ITEM IN THIS LINE
   set the dgtext of grp 1 to it
end mouseup
Now click btn 1. Data is loaded. Now click btn 2. No problem, right? OK, reload from btn 1, but now change the item to "4" in the script of btn 2. Save, and click btn 2 again. You might play around with other item references...

What Klaus meant was you have to watch your item management.

Is this clear? It might not be. Play around a bit and write back if you need to.

Craig Newman

altamative131
Posts: 7
Joined: Mon Jan 13, 2014 6:57 pm

Re: Datagrids -- make my head hurt

Post by altamative131 » Thu Jan 23, 2014 8:00 pm

Craig,

That all makes perfect sense. I am just trying to figure out how I can force it to choose the next empty line in the datagrid.

I basically want it to look something like this.

if line 1 of item 3 is empty then put the text of field "Name" into line 1 of item 3
else put text of field "name" into next empty line of item 3


Thanks

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

Re: Datagrids -- make my head hurt

Post by dunbarx » Thu Jan 23, 2014 8:27 pm

DG's are essentially tab and return delimited text containers. The data is held internally in an array, but as you have seen, there is a way to get, and set, that data in the clear, using the "dgText".

You are mixing lines and items, I think. Though it is syntactically permitted to have several lines within an item, this is not how DG's are structured. The lines contain items, not the other way around. So you should not want to:
if line 1 of item 3 is empty then put the text of field "Name" into line 1 of item 3
Do I have this right? I assume you want sort of the reverse, "if line 3 is empty then put yourNewData into line 3..."

If so, then when you extract the current data using the dgText, you can find the number of lines of that block:

Code: Select all

get the dgText of grp "yourDG"
put the number of lines of it into numLines
put yourTabDelimitedData into line (numLines + 1) of it
set the dgText of grp "yourDG" to it
Write back if I am missing what you mean.

Craig

Post Reply