How to populate the controls on a DataGrid Form

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
Traxgeek
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 281
Joined: Wed Jan 09, 2013 10:11 am

How to populate the controls on a DataGrid Form

Post by Traxgeek » Sun Jan 13, 2013 11:18 am

Hi again,

My 'successess' seem to remain very short lived - IF indeed, they were ever successes I could loosely claim ownership to in the first instance ! (So far, I need to thank Klaus for showing me the path on each occasion... :oops: ) - Thanks again Klaus. Having said that, this remains a painful and rather steep learning curve even with webcasts, books, dictionaries, help files and the forum etc... :(

My problem now is how to populate some controls (labels and an image) within a DataGrid set up as a 'Form' from an SQLite Database...

I have my SQLite database which holds my data (so far just two items consisting of database ID, Name, Address and Image - the image field is by way of a path to the 'SpecialFolder' & "\Images" folder)
Thanks Klaus
I have set up the DataGrid (I think :? ) as a form called 'dgPeople' and placed a group (dgPeople) of controls into it ('lblID', 'lblName', 'btnImage' and 'lblAddress')
Thanks Datagrid webcast - courtesy team Revolution and example video/stack - courtesy LiveCode

I can query the SQLite Database - and can 'see' the results (break point in my code) going into a local 2 dimensional Array variable called 'sDataArray'

After finishing the SQL query and subsequent array population I send the data to my Datagrid with this call

Code: Select all

set the dgData of group "dgPeople" to sDataArray
I can then 'trap' this call at the 'FillInData pDataArray' behaviour script attached to my DataGrid.
All well so far (This took me the best part of yesterday :( but at least I can 'see' the data entering the behaviour script by placing a breakpoint in it :)
My problem starts when I try to populate my four Datagrid controls with the data in 'pDataArray'.
Firstly I 'expected' the 'FillInData' code to be called once with the full array of data 'pDataArray' but, it would appear that, in reality we seem to jump into this code the same number of times that we have data lines (records) for... I've ammended my Datagrid population cotrol for this and it ends up as :

Code: Select all

put pDataArray[1] into the field "lblID" of me
put pdataArray[2] into the field "lblName" of me
set pDataArray[3] into the icon of control "btnImage" of me
put pDataArray[4] into the field "lblAddress" of me
When I 'Apply' these code lines to the Behaviour Script I receive a big green tick and no errors :D
However, when LC attempts to run this script I receive the following (somewhat cryptic - to me!) error :

button "Behavior Script": execution error at line 8 (Chunk: no such object) near "lblID", char 22

with execution halted on line 1 (put pDataArray[1] into the field "lblID" of me) of my four code lines (above)...

I have checked that lblID exists in my Datagrid and that my data is intact (within 'pDataArray') at this point and have attempted various mutations of
put pDataArray[1] into the field "lblID" of me
such as
put pDataArray[1] into field "lblID" of me
put pDataArray[1] into the control "lblID" of me
put pDataArray[1] into control "lblID" of me

plus every other option I can think of.
I have watched and re-watched the webcasts
I have worked my way through the examples and .... arghhhh !
I have spent HOURS at this point and gotten nowhere !

I'm quite obviously missing / overlooking some vital point(s) but... what / which one(s).

Can anyone assist please ?

Thanks in advance.
I'm 'getting there'... just far too slowly !
Mac (Siera) and PC (Win7)
LiveCode 8.1.2 / 7.1.1

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

Re: How to populate the controls on a DataGrid Form

Post by Klaus » Sun Jan 13, 2013 12:35 pm

Hi Traxgeek,

1. do not use THE with control references!!!!
Correct: put X into fld 1
Wrong: put X into THE fld 1

2. the KEYS of the pDataArray are named as the fields in your datagrid form, so use this:
...
put pDataArray["lblID"] into field "lblID" of me
put pdataArray["lblName"] into field "lblName" of me

## SYNTAX for setting icon of button!
set the icon of control "btnImage" of me to pDataArray["btnImage"]
put pDataArray["lblAddress"] into field "lblAddress" of me
...

Best

Klaus

Traxgeek
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 281
Joined: Wed Jan 09, 2013 10:11 am

Re: How to populate the controls on a DataGrid Form

Post by Traxgeek » Sun Jan 13, 2013 3:04 pm

Hi Klaus,
Thanks for that (especially on a Sunday). This is (again) driving me nuts :twisted: but...
I have tried your code snippets / instructions ad nauseam but to no avail...

In the hope that the following (1) helps you (or anyone else) help me and (2) helps anyone else in the same pickle in the future as me now, I have included a pretty full code sample below...

Code: Select all

--Query the DB
    local sDBQuery, sDBData    
    put "SELECT PID, Name, Image, Adress FROM DBPeople" into sDBQuery
    put revDataFromQuery(,,nDBConnectionID, sDBQuery) into sDBData

--Populate the Data Array
    local R, F #Breaking and looking at sDBData here displays the People data as I would expect
    set the itemdelimiter to tab
    set the linedelimiter to cr
    put 0 into R
    repeat for each line sLine in sDBData
        add 1 to R
        put 0 into F
        repeat for each item sItem in sLine
            add 1 to F
            put sItem into sDataArray[R][F]
            put false into sDataArray[R]["expanded"]    #Don't know WHY I do this but the example showed it with no real explanation.
                                                                            #Did look up 'expanded' in the dictionary...
                                                                            #Seems to have little to do with what I'm trying to achieve
                                                                            #(and I get the same results with or without it) but...
        end repeat
    end repeat
    
--Populate the DataGrid
    set the dgData of group "dgProperties" to sDataArray   #Breaking and looking at sDataArray here also displays the data I'd expect
                                                                                    #(I have two distinct entries/records in the Array here

--Behaviour Script (Chunk 1)
    on FillInData pDataArray
        put pDataArray[1] into field "lblID" of me     #Remarking out out all lines in this snippet and simply typing 
                                                                         #local S: put pDataArray[1 [to 4] ] into S
                                                                         #displays my data for each record here 
        put pdataArray[2] into field "lblName" of me
        set the icon of control "btnImage" of me to specialfolder("Documents") & "\Images" & "\" & pDataArray[3]
        put pDataArray[4] into field "lblAddress"
    end FillInData

or Behaviour Script (Chunk 2)
    on FillInData pDataArray
        put pDataArray["lblID"] into field "lblID" of me
        put pdataArray["lblName"] into field "lblName" of me
        set the icon of control "btnImage" of me to specialfolder("Documents") & "\Images" & "\" & pDataArray[3]
        put pDataArray["lblAddress"] into field "lblAddress" of me
    end FillInData

or Behaviour Script (Chunk 3)
    on FillInData pDataArray
        put pDataArray["Col1"] into field "lblID" of me
        put pdataArray["Col2"] into field "lblName" of me
        set the icon of control "Col3" of me to specialfolder("Documents") & "\Images" & "\" & pDataArray[3]
        put pDataArray["Col4"] into field "lblAddress" of me
    end FillInData

All Behaviour Script chunks 'Apply' OK but throw the following error when LC attempts to run them
button "Behavior Script": execution error at line 8 (Chunk: no such object) near "lblID", char 22

I have 'obviously' (? !) done something wrong when creating my Datagroup I guess.

My biggest 'problem' is that I don't understand the error message so I am at a loss as to where to go now... except back to the Datagrid and pore over that but the 'force' remains weak with this one ! and I remain lost in some dark dank swamp of code that I simply don't understand the relevence of ! Apologies.

I'll go back to the Datagrid webcast (my concern here tho' is that it was written in the darker days of 'Revolution' and I'm unsure as to its continued relevance in all directions/nuances of LC...)

It's probably worth mentioning that if I exit run mode, select the datgrid and change the 'Style' from 'Form' to 'Table' I immediately 'see' my 2 People data records appear in the first two rows of the Datagrid (now in 'Table' Style) in the IDE... might be of some relevance... might not ! The image of a dying man and numerous straws springs to mind !!!
I'm 'getting there'... just far too slowly !
Mac (Siera) and PC (Win7)
LiveCode 8.1.2 / 7.1.1

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

Re: How to populate the controls on a DataGrid Form

Post by Klaus » Sun Jan 13, 2013 4:06 pm

Hi Traxgeek,

1. you can force the data to be returned as TAB and CR delimited when you pass this information to the revdatafromquery command:
...

Code: Select all

local sDBQuery, sDBData    
put "SELECT PID, Name, Image, Address FROM DBPeople" into sDBQuery
put revDataFromQuery(TAB,CR,nDBConnectionID, sDBQuery) into sDBData
...
2. problem is that you do not create an Array with corresponding (to the FILLINDATA handler) key NAMES
but instead also create NUMERIC keys in the 2nd dimension of your array.
Do this:

Code: Select all

...
local R
set the itemdel to tab

## This is the default -> CR, so not neccessary to set it: 
## set the linedelimiter to cr

## Numeric key OK for ROWS!
put 0 into R

## Here you need to NAME the keys!
## Order or your returned data from db:  PID, Name, Image, Address
repeat for each line sLine in sDBData
       add 1 to R
       put item 1 of sLine into sDataArray[R]["pid"]
       put item 2 of sLine into sDataArray[R]["name"]
       put item 3 of sLine into sDataArray[R]["image"]
       put item 4 of sLine into sDataArray[R]["address"]
end repeat
...
Then in the FILLINDATA handler you need to address the correct field of your datagrid:

Code: Select all

on fillinata pDataArray
   put pDataArray["pid"] into field "lblID" of me
   put pdataArray["name"] into field "lblName" of me

  ## Presuming that this is a BUTTON where you set its ICON to the id of any image!?
   set the icon of BTN "image" of me to pDataArray["btnImage"]
   put pDataArray["address"] into field "lblAddress" of me
end fillindata
Best

Klaus

Traxgeek
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 281
Joined: Wed Jan 09, 2013 10:11 am

Re: How to populate the controls on a DataGrid Form

Post by Traxgeek » Sun Jan 13, 2013 6:54 pm

Hi Klaus,

Still struggling I'm afraid :(

Still getting the error
button "Behavior Script": execution error at line 13 (Chunk: no such object) near "lblID", char 26

If I leave your code but add some of my own checks :

YOUR CODE
on fillinata pDataArray
put pDataArray["pid"] into field "lblID" of me
#ERROR OCCURS AS SOON AS WE GET TO THIS LINE
put pdataArray["name"] into field "lblName" of me

## Presuming that this is a BUTTON where you set its ICON to the id of any image!? - THAT'S MY IDEA - YES !
set the icon of BTN "image" of me to pDataArray["btnImage"]
put pDataArray["address"] into field "lblAddress" of me
end fillindata


MY CODE (This isn't to see mhy I have the error - just to give me a wee sanity check that I DO in fact have my data in the format I expect)
on fillinata pDataArray
local S1, S2, S3, S4
put pDataArray["pid"] into S1
put pDataArray["name"] inti S2
put pDataArray["address"] in S3
put S1 & cr & S2 & cr & S3 into S4
answer info S4


put pDataArray["pid"] into field "lblID" of me
#ERROR OCCURS AS SOON AS WE GET TO THIS LINE
put pdataArray["name"] into field "lblName" of me

## Presuming that this is a BUTTON where you set its ICON to the id of any image!? - THAT'S MY IDEA - YES !
set the icon of BTN "image" of me to pDataArray["btnImage"]
put pDataArray["address"] into field "lblAddress" of me
end fillindata


I have (naturally enough, I'm sure, but great for my sanity) two occurrences of an Info box with an <OK> button pop up one after the other - each with one record as per the DB. Pretty much as I'd hoped / expected... So (to me) my data is OK, my code to populate / build my data array is OK and my 'extraction' is OK. It's 'just' referencing those controls...

The code still doesn't like my referencing my controls in my Datagird group. Like I say, 1 Datagrid (called 'dgPeople'), 4 controls called 'lblID', 'lblName', 'lblAddress' and 'btnImage' all grouped under the same name as my DataGrid control, 'dgPeople'...

So, all I've managed to do is prove my data does exist where I want it (sDataArray) in the format I want it but (apart from calling an 'answer info') I can't display it in my Datagrid...

I'm sure this is something soooooo very very basic I'm overlooking it completely but for the life of me I can't figure it out ! :twisted:

I've recreated my Datagrid from scratch a dozen times, I've then recreated my code to populate the sDataArray and the 'FillInData' routine the same number of times but to no avail...

Any further ideas please ? Thanks...
I'm 'getting there'... just far too slowly !
Mac (Siera) and PC (Win7)
LiveCode 8.1.2 / 7.1.1

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

Re: How to populate the controls on a DataGrid Form

Post by Klaus » Sun Jan 13, 2013 7:47 pm

4 controls called 'lblID', 'lblName', 'lblAddress' and 'btnImage'
You added these fields/buttons to the "TEMPLATE GROUP" in the stack with the datagrid TEMPLATES
after a click on "Row Template" in the inspector for your datagrid, right?

And the TYPE of your datagrid is "FORM", right?

Traxgeek
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 281
Joined: Wed Jan 09, 2013 10:11 am

Re: How to populate the controls on a DataGrid Form

Post by Traxgeek » Mon Jan 14, 2013 8:01 am

Hi Klaus,

Thanks, Yes and Yes

... or, at least, I think so !

I'm going to go away, make a new Datagrid from scratch and make notes as I go that I'll post here once I'm done !

Many thanks. I'll revert shortly (I hope)
I'm 'getting there'... just far too slowly !
Mac (Siera) and PC (Win7)
LiveCode 8.1.2 / 7.1.1

Traxgeek
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 281
Joined: Wed Jan 09, 2013 10:11 am

Re: How to populate the controls on a DataGrid Form

Post by Traxgeek » Mon Jan 14, 2013 9:48 am

Hi,

So this is EXACTLY what I am doing (HTH) :

-1- Drag a Datagrid from the toolbar onto my Card
-2- Leave the name of the Datagrid 'as-is' = 'DataGrid 2'
-3- Change default DG style to 'Form'
-4- Select <Row Template...>
-5- Select the blue 'background' bar at the top of the "Editing template for group 'DataGrid 2" of 'crdPeople'.... pop up
-6- Select <Ungroup> from the main menu (and note that the single entity within the popup changes to 2 entities : one is called 'Background', the other is called 'Label')
-7- Rename the 'Label' to 'lblID' and resize it (make it smaller
-8- Drag out two more labels from the toolbar and place them next to 'lblID'.
-9- Name these two new labels 'lblName' and 'lblAddress'
-10- Drag out a button, place it next to my other labels and rename it to 'btnImage'
-11- Highlight the 'Background' control and drag the red handles so that it extends to 'cover' all of the other controls ('lblID', 'lblName', 'lblAddress' and 'btnImage') - not really sure at this point why I want / need a 'Background' but I've left it in)
-12- Select all the controls and then select <Group>. The multitude of red handles are replaced by one set about my group, which has taken the name 'Row Template'
-13- Select the red 'X' (top right hand corner of the popup) to close it and select <Save> in the dialogue that appears.
-14- Reselect my Datagrid ( 'DataGrid 2')
-15- Select <Row Behaviour...> and... the 'Button Behaviour' script in the code window
NOTE : If I miss out the 'Group' section ( -12- above ) selecting <Row Behaviour...> has absolutely no effect. Nothing happens. Selecting <Row Behaviour...> once the group is created howeverdisplays the 'Button Behaviour' script in the code window...
-16- Cut and paste our previous 'Button Behaviour FillInData pDataArray' code into the 'Button Behaviour FillInData pDataArray' area of this Datagrid ('DataGrid 2') so...

Code: Select all

on FillInData pDataArray
   local S1, S2, S3, S4, S5
   put pDataArray["PID"] into S1
   put pdataArray["Name"] into S2
   put pDataArray["Image"] into S3
   put pDataArray["Street1"] & cr & pDataArray["Street2"] & cr & pDataArray["City"]  & cr & pDataArray["County"] & cr & pDataArray["Postcode"] into S4
  
   put S1 & cr & S2 & cr & S3 & cr & S4 into S5
   answer info S5
   
  
   put pDataArray["PID"] into field "lblID" of me
   put pdataArray["Name"] into field "lblName" of me
end FillInData
So, the moment of truth... run the code...
The <Info> dialogue appears with my data in it :)
... hmmm... but then I get a <Popup> declaring:
An error has occurred in behaviour for the row template :
Chunk: no such object


When I select <Edit Script> from within that dialogue I come back to my first label population line of my FillInData script :
put pDataArray["PID"] into field "lblID" of me

Frustrating... :twisted:

Ideas please ?
I'm 'getting there'... just far too slowly !
Mac (Siera) and PC (Win7)
LiveCode 8.1.2 / 7.1.1

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

Re: How to populate the controls on a DataGrid Form

Post by Klaus » Mon Jan 14, 2013 12:15 pm

No more "remote" ideas! Can you send me the stack, so I can take a look?
-> klaus AT major-k.de

Post Reply