Saving datagrid data and placing it back

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
mrcoollion
Posts: 720
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

Saving datagrid data and placing it back

Post by mrcoollion » Thu Jul 13, 2017 7:55 pm

In my application I fill a datagrid (DG) with a collection of numbers

What I want to do it to save this collection including the order into an Array, empty the DG and fill the DG with a new collection.
Later I want to place a saved collection back into this DG and in the same order as I saved it.

I thought that would be straight forward by doing it the following way but this does not work?

First save the DG data into an array named ArrayCollection1:

Code: Select all

 put the dgData of group "NumbersCollection" into ArrayCollection1
And when I need to put the data in ArrayCollection1 back into the datagrid i do:

Code: Select all

put ArrayCollection1 into theDataA

put  1 into theLineNo
        dispatch "AddData" to group "NumbersCollection" with theDataA, theLineNo
        put the result into theNewIndex -- integer if successful, error string otherwise
        set the dgVScroll of group "NumbersCollection" to theLineNo
However this does not work :cry:

What am I doing wrong and how to achieve this as easy as possible?

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

Re: Saving datagrid data and placing it back

Post by Klaus » Thu Jul 13, 2017 8:02 pm

Dag Paul,,

"addData" will only work with ONE record, but you said you want ot REPLACe the content of the datagrid and not ADD something.
In that case you have to SET the same property that you have GET (gotten) first:

Code: Select all

...
## Not really neccessary
## put ArrayCollection1 into theDataA
set the dgdata of grp "NumbersCollection" to ArrayCollection1
...
No need for scrolling anything.


Best

Klaus

mrcoollion
Posts: 720
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

Re: Saving datagrid data and placing it back

Post by mrcoollion » Thu Jul 13, 2017 8:27 pm

Hello Klaus,

Thanks! This did the trick.
It was even simpler than I thought
(bedankt :D )

mrcoollion
Posts: 720
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

Re: Saving datagrid data and placing it back

Post by mrcoollion » Thu Jul 13, 2017 9:01 pm

After some testing i found out that the order in which the data is placed back into the DG, is in the order of the data (numbers) and not in the order the user entered the data.
So 3 2 1 becomes 1 2 3 after placing it back into the DG.
I need the DG to be filled exactly the same as it was with the numbers in the same order.

I know that I can get the indexes

Code: Select all

put the dgIndexes of group "NumbersCollection" into IndexesArrayCollection1
But how can I use this to put the data back into the DG in the correct order?

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

Re: Saving datagrid data and placing it back

Post by Klaus » Thu Jul 13, 2017 9:25 pm

If your datagrid is of type TABLE you could try to GET and SET -> the dgtext of grp XYZ
That is just TAB and CR delimited text and is exactly what you see in the datagrid.

mrcoollion
Posts: 720
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

Re: Saving datagrid data and placing it back

Post by mrcoollion » Thu Jul 13, 2017 9:29 pm

It is a form but only with one field.

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

Re: Saving datagrid data and placing it back

Post by Klaus » Thu Jul 13, 2017 10:22 pm

If the field will only contain a single line, then "dgtext" should work here, too.

mrcoollion
Posts: 720
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

Re: Saving datagrid data and placing it back

Post by mrcoollion » Fri Jul 14, 2017 7:27 am

Found the solution which is the same as with the data.

First put the indexes in a variable like this :

Code: Select all

put the dgIndexes of group "NumbersCollection" into IndexesArrayCollection1
After having placed the data back, then set the indexes of the DG tot the saved indexes like this:

Code: Select all

set the dgIndexes of group "NumbersCollection" to IndexesArrayCollection1
Probably a bit to late yesterday that I did not think of this sooner. :shock:

Thanks Klaus!

PS. for those who actually want to save the array to a file. Read this:
http://lessons.livecode.com/s/lessons/m ... e-it-again

Post Reply

Return to “Talking LiveCode”