Getting values from Selected Datagrid row

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: Klaus, FourthWorld, heatherlaine, kevinmiller

Post Reply
gagsoft
Posts: 168
Joined: Sat Jun 29, 2013 7:56 pm

Getting values from Selected Datagrid row

Post by gagsoft » Tue Sep 17, 2019 2:10 pm

Hi guys
I get "group "MyGrid": execution error at line n/a (Object: object does not have this property)" from the datagrid.
The goal is to edit a record.
Here is the code:
on selectionChanged
put the hilitedline of me into tLine
// put the dgline of me into tLine
Answer tLine
put item 1 of line tLine of me into field "id"
put item 2 of line tLine of me into field "Client"
put item 3 of line tLine of me into field "Contact"
put item 4 of line tLine of me into fld "Phone"
put item 5 of line tLine of me into fld "Issue"
put item 6 of line tLine of me into fld "Issuedate"
put item 7 of line tLine of me into fld "Remedy"
put item 8 of line tLine of me into fld "RemedyDate"
end selectionChanged
Would appreciate any pointers here as am still testing the waters here

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

Re: Getting values from Selected Datagrid row

Post by Klaus » Tue Sep 17, 2019 2:17 pm

Hi gagsoft,

well almost:

Code: Select all

...
## FIELDS do have this property -> hilitedline
## GROUPS (datagrids) do NOT!
## put the hilitedline of me into tLine

## This is the one:
put the DGhilitedline of me into tLine
...
Best

Klaus

gagsoft
Posts: 168
Joined: Sat Jun 29, 2013 7:56 pm

Re: Getting values from Selected Datagrid row

Post by gagsoft » Tue Sep 17, 2019 2:23 pm

Hi Klause
Many thanks
Much appreciated :D

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

Re: Getting values from Selected Datagrid row

Post by Klaus » Tue Sep 17, 2019 2:29 pm

Klaus, without an E!
If in doubt, just copy and paste my name. 8)

Zryip TheSlug
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 161
Joined: Tue Jan 26, 2010 10:15 pm
Contact:

Re: Getting values from Selected Datagrid row

Post by Zryip TheSlug » Tue Sep 17, 2019 5:09 pm

Hi Gagsoft,

Have a look to this lesson:
http://lessons.livecode.com/m/datagrid/ ... -or-column

Note the dghilitedline is returning the number of the selected line, not the content data of the corresponding row.
You need to deal with the array notation for that.

Code: Select all

put the dghilitedline of me into tLine
put the dgDataOfLine[tLine] of me into theDataA
put theDataA["id"] into field "id"
put theDataA["client"] into field "client"
etc
Note each keys of the array returned by the dgDataOfLine property is columns names of your datagrid: id, client, contact, etc


Best Regards,
TheSlug
http://www.aslugontheroad.com - Tutorials, demo stacks and plugins for LiveCode
Data Grid Helper - An intuitive interface for building LiveCode's Data Grids
Excel Library- Extends the LiveCode language for controlling MS Excel

gagsoft
Posts: 168
Joined: Sat Jun 29, 2013 7:56 pm

Re: Getting values from Selected Datagrid row

Post by gagsoft » Wed Sep 18, 2019 7:17 pm

Hi guys

I tested this:
on selectionChanged
put the DGhilitedline of me into tLine -- This works
Answer Tline
put the dgDataOfLine[tLine] of me into theDataA
//The following does not yield anything
put theDataA["id"] into field "id"
put theDataA["client"] into field "client"
end selectionChanged

Am I missing something here?

Zryip TheSlug
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 161
Joined: Tue Jan 26, 2010 10:15 pm
Contact:

Re: Getting values from Selected Datagrid row

Post by Zryip TheSlug » Wed Sep 18, 2019 7:40 pm

This should work. Do your columns are named "id" and "client"? Don't confuse column name and column label.

Try with a fresh datagrid and three new columns:

Both codes:

Code: Select all

on selectionChanged
   local tLine, theDataA
   
   put the dghilitedline of me into tLine
   put the dgDataOfLine[tLine] of me into theDataA
   put theDataA["col 1"] into field "col1"
   put theDataA["col 2"] into field "col2"
   put theDataA["col 3"] into field "col3"
end selectionChanged
And the more optimized (because selectionChanged is receiving two parameters sent by the datagrid API: the clicked index and the previous selected index)

Code: Select all

on selectionChanged pHilitedIndex, pPrevHilitedIndex
   local theDataA
   
   put the dgDataOfIndex[pHilitedIndex] of me into theDataA
   put theDataA["col 1"] into field "col1"
   put theDataA["col 2"] into field "col2"
   put theDataA["col 3"] into field "col3"
end selectionChanged
Note, this code is something similar to the example for the selectionChanged readable in the datagrid API (topic = "Messages Sent"):
http://lessons.livecode.com/m/datagrid/ ... a-grid-api


Are working well. I'm using this with different datagrids.
TheSlug
http://www.aslugontheroad.com - Tutorials, demo stacks and plugins for LiveCode
Data Grid Helper - An intuitive interface for building LiveCode's Data Grids
Excel Library- Extends the LiveCode language for controlling MS Excel

gagsoft
Posts: 168
Joined: Sat Jun 29, 2013 7:56 pm

Re: Getting values from Selected Datagrid row

Post by gagsoft » Wed Sep 18, 2019 8:20 pm

Thank you guys
voila.. It works like a charm.
Klaus my apologies about the typo☺...will not happen again.

Many thanks 👌

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”