How to copy a column of data from a datagrid table.

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

CAsba
Posts: 364
Joined: Fri Sep 30, 2022 12:11 pm

How to copy a column of data from a datagrid table.

Post by CAsba » Fri Jun 02, 2023 10:13 am

Hi,
How to copy a column of data from a datagrid table to another datagrid table ? Can't find anything in the lessons that deals with this.

This is the context, if it helps..

I have datagrid 2, that contains all data on products. From this datagrid table, I want to extract a few of its columns to put into another datagrid (table) stocktake. This table does not require all the data in datagrid 2.

Incidentally, the datagrid stocktake has columns for data that datagrid 2 does not have; the data for these columns are to be entered following a physical stocktaking, and this datagrid, stocktake, will be printed, to be used as a stocktaking check list.

I hope this explains what I am trying to achieve..

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

Re: How to copy a column of data from a datagrid table.

Post by Klaus » Fri Jun 02, 2023 12:05 pm

Hi CAsba,

do you want to copy one column or all columns of datagrid 1?
And do you already know how to assign the copied column(s) to the correct indexes/lines of datagrid 2?

Best

Klaus

CAsba
Posts: 364
Joined: Fri Sep 30, 2022 12:11 pm

Re: How to copy a column of data from a datagrid table.

Post by CAsba » Fri Jun 02, 2023 1:13 pm

Hi Klaus,
Many thanks for your reply.
I want to copy 5 columns into the other DG. I thought I would script it to copy one column at a time.
Not too sure what you mean in your second question. I thought I would put the copied column into the DG by the 'enter data' method, but I haven't tried it yet, so no, I don't know how.. 'I will cross that bridge when I get to it' was my strategy (!)

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

Re: How to copy a column of data from a datagrid table.

Post by Klaus » Fri Jun 02, 2023 1:59 pm

Hi CAsba,
CAsba wrote:
Fri Jun 02, 2023 1:13 pm
Not too sure what you mean in your second question.
I thought you already have some data in datagrid 2 and need to assign the copied column data to the appropriate index/line.
OK, basically you need to:

Code: Select all

...
## Get the data from the wanted column from the first datagrid:
## You could also use -> put the dgDataOfLine[your LINE number here]...
put the dgDataOfIndex[your index number here] of grp "datagrid 1" into tDataArray
put tDataArray["name of your column here"] into tData2Bcopied

## Get the index/line you want to copy the data to:
put the dgDataOfIndex[your index number here] of grp "datagrid 2" into tDataArray2

## Write column data to this array:
put tData2Bcopied into tDataArray2["name of target column here"]

## Write array back to datagrid 2:
set the dgDataOfIndex[your index number here] of grp "datagrid 2" to tDataArray2
...
Best

Klaus

CAsba
Posts: 364
Joined: Fri Sep 30, 2022 12:11 pm

Re: How to copy a column of data from a datagrid table.

Post by CAsba » Fri Jun 02, 2023 5:43 pm

Hi Klaus,
I tried it but without success.
Also, I'm not sure if I've got my problem across to you correctly.
I may be wrong, but I suspect that the code you gave me would transfer ONE line/column intersection to the second DG; I am trying to transfer the whole column, with lots of data in it, to the second DG.
Does this make sense ?

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

Re: How to copy a column of data from a datagrid table.

Post by Klaus » Sat Jun 03, 2023 10:15 am

Hi CAsba,

yo, sounds like I misunderstood you.
OK, maybe this is better. :-)

Code: Select all

...
## Get the data array of datagrid 1 to work with:
put the dgdata of grp "datagrid 1" into tData

## Extract all wanted columns into another array
## How many lines?
put the num of keys of tData into tNumOfKeys
repeat with i = 1 to tNumOfKeys

  ## 1. put the extracted columns into an array:
  put tData[i]["name of wanted colum"] into tColumnArray[i]["name of wanted colum"]

  ## 2. Alternatively, if you need a CR delimited text list?
  ## put tData[i]["name of wanted colum"] & CR after tListOfColumns
end repeat
## Get rid of trailing CR
## delete char -1 of tListOfColumns
...
Since i don't know how you want to put the extracted columns into the other datagrid
I need more info about that. But maybe this already helps.

Best

Klaus

CAsba
Posts: 364
Joined: Fri Sep 30, 2022 12:11 pm

Re: How to copy a column of data from a datagrid table.

Post by CAsba » Sat Jun 03, 2023 12:08 pm

Thanks Klaus, I'll try that, it will have to be on Monday...

CAsba
Posts: 364
Joined: Fri Sep 30, 2022 12:11 pm

Re: How to copy a column of data from a datagrid table.

Post by CAsba » Sat Jun 03, 2023 8:38 pm

Hi Klaus,
I just tried it..Not working. It seems to be the first line, as I tried to put tdata into fld "dgdg" to see if anything was in tdata,

Code: Select all

put the dgdata of grp "datagrid 1" into tData
   put tdata into fld "dgdg"

and nothing was put into the field.

stam
Posts: 2682
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: How to copy a column of data from a datagrid table.

Post by stam » Sun Jun 04, 2023 12:31 am

CAsba wrote:
Fri Jun 02, 2023 10:13 am
Hi,
How to copy a column of data from a datagrid table to another datagrid table ? Can't find anything in the lessons that deals with this.

This is the context, if it helps..

I have datagrid 2, that contains all data on products. From this datagrid table, I want to extract a few of its columns to put into another datagrid (table) stocktake. This table does not require all the data in datagrid 2.

Incidentally, the datagrid stocktake has columns for data that datagrid 2 does not have; the data for these columns are to be entered following a physical stocktaking, and this datagrid, stocktake, will be printed, to be used as a stocktaking check list.

I hope this explains what I am trying to achieve..
I may have misunderstood, but if not, you want some columns in datagrid1 to show in datagrid2?
You could go through a process which what is probably outlined above of creating a column of text or an array out of data you want to display?

But why do that at all?

I can't think of any reason why you can't just

Code: Select all

set the dgData of datagrid2 to the dgData of datagrid1
and to display the data, only create the columns you want to display in the data grid. It will only show the data who's column names match the keys of the array, any other array data will not be displayed...

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

Re: How to copy a column of data from a datagrid table.

Post by Klaus » Sun Jun 04, 2023 12:42 am

CAsba wrote:
Sat Jun 03, 2023 8:38 pm
Hi Klaus,
I just tried it..Not working. It seems to be the first line, as I tried to put tdata into fld "dgdg" to see if anything was in tdata,

Code: Select all

put the dgdata of grp "datagrid 1" into tData
   put tdata into fld "dgdg"

and nothing was put into the field.
tData (the dgdata of grp xyz) is an ARRAY!
If you want to see the data in a field use -> the dgText of grp xyz

stam
Posts: 2682
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: How to copy a column of data from a datagrid table.

Post by stam » Sun Jun 04, 2023 1:07 am

If you're set on having 'pure' data so the datagrid2 only has the column data you specify (as opposed to all the data and just showing some, as I posted above), you could write a short function as below to create the array for dataGrid2.
In the code below, pSourceLongID is the long id of your source data grid that contains the superset of data and pColumnList is a list of the column names (not labels) you want to extract and display in datagrid2 (code not tested):

Code: Select all

function getSubArray pSourceLongID, pColumnList
    local tSourceArray, x = 0, tKeys, tSubArray
    put the dgData of pSourceLongID into tSourceArray
    repeat for each element tSourceRow in tSourceArray
        add 1 to x
        put the keys of tSourceRow into tKeys
        repeat for each line tKey in tKeys
            if tKey is in pColumnList then
                put tSourceRow[tkey] into tSubArray[x][tKey]
            end if
        end repeat
    end repeat
    return tSubArray
end getSubArray
You would then set the dgData of datagrid 2 to the output of this function:

Code: Select all

 set the dgData of datagrid2 to getSubArray(the long id of datagrid1, "col1, col2, ..., colN") 
where datagrid1 is the source datagrid and "col1, col2,..., colN" are the column names you want to extract from datagrid 1 and display in datagrid 2.

However, I still think my previous suggestion of simply setting the dgData to the superset that is in datagrid1 but only displaying the columns you want in datagrid2 is more practical...

HTH
Stam

--------------
Edit: gave variables more meaningful names and corrected a minor error as I forget 'the keys' creates a return, rather than comma, delimited list...

CAsba
Posts: 364
Joined: Fri Sep 30, 2022 12:11 pm

Re: How to copy a column of data from a datagrid table.

Post by CAsba » Sun Jun 04, 2023 12:19 pm

Hi,
Your idea, Stam, of copying the whole data into a DG with only some columns showing seems a pretty good solution. Many thanks, I'll try it..

CAsba
Posts: 364
Joined: Fri Sep 30, 2022 12:11 pm

Re: How to copy a column of data from a datagrid table.

Post by CAsba » Sun Jun 04, 2023 12:40 pm

Hi Stam,
I tried it - it worked ! - many thanks.

stam
Posts: 2682
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: How to copy a column of data from a datagrid table.

Post by stam » Sun Jun 04, 2023 12:46 pm

Glad it helped! Also note that you don’t even need two data grids.

If the data grid contains the superset array of data you can simply change the dgProps[“colunns”] to only show the columns you want, so you could sett up buttons or a switch widget to toggle between the two for example - but not sure what suits your app best…

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

Re: How to copy a column of data from a datagrid table.

Post by Klaus » Sun Jun 04, 2023 1:13 pm

CAsba,

as you may have noticed, you cannot put an ARRAY into a field, OK, you actually can, but it will not display anything. 8)
If you want to see/visualize an ARRAY, you can use a "treeview" widget:

Code: Select all

...
set the arraydata of widget "your treeview widget here" to the dgData of grp "your datagrid here"
...
Best

Klaus

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”