Page 1 of 1

Saving datagrid data and placing it back

Posted: Thu Jul 13, 2017 7:55 pm
by mrcoollion
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?

Re: Saving datagrid data and placing it back

Posted: Thu Jul 13, 2017 8:02 pm
by Klaus
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

Re: Saving datagrid data and placing it back

Posted: Thu Jul 13, 2017 8:27 pm
by mrcoollion
Hello Klaus,

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

Re: Saving datagrid data and placing it back

Posted: Thu Jul 13, 2017 9:01 pm
by mrcoollion
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?

Re: Saving datagrid data and placing it back

Posted: Thu Jul 13, 2017 9:25 pm
by Klaus
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.

Re: Saving datagrid data and placing it back

Posted: Thu Jul 13, 2017 9:29 pm
by mrcoollion
It is a form but only with one field.

Re: Saving datagrid data and placing it back

Posted: Thu Jul 13, 2017 10:22 pm
by Klaus
If the field will only contain a single line, then "dgtext" should work here, too.

Re: Saving datagrid data and placing it back

Posted: Fri Jul 14, 2017 7:27 am
by mrcoollion
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