GetDataOfIndex for a DataGrid

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
lohill
Posts: 770
Joined: Tue Dec 08, 2009 6:37 pm

GetDataOfIndex for a DataGrid

Post by lohill » Sat Dec 26, 2009 12:57 am

Using the example on Page 66 of the BlueMango DataGrid Guide, I have tried to test the use of GetDataOfIndex. I have a card with one button called "Test" and a DataGrid called "DataGrid 1". The data grid contains about 8 rows of data which appear to be relatively happy being in the grid. That is to say columns can be sorted and resized. The button contains the following script:

Code: Select all

on mouseUp
   put the dgHilitedIndexes of group "DataGrid 1" into theIndex 
   answer theIndex
   put GetDataOfIndex(theIndex, "Ticker") into theColValue     -- the heading for column 1 is "Ticker"
   answer theColValue
end mouseup
When this code is run the index of the hilited line is displayed but the code stops at line 4 with the following:

button "Test": execution error at line 4 (Function: error in function handler) near "GetDataOfIndex", char 8

Should this code be runnable from a button or does it have to be placed somewhere else? I had expected to have "BP" displayed.

Any help would be appreciated.
Thanks - Larry

dickey
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 118
Joined: Wed Apr 08, 2009 11:54 pm

Re: GetDataOfIndex for a DataGrid

Post by dickey » Sat Dec 26, 2009 11:02 pm

Hello Larry,

A summary of functions to obtain the index of the selected (hilited) line or lines and the value of individual columns - of a DataGrid.

In situations where you wish to program functionality based on the currently hilited row (singular), use a handler such as this, in the script of the DataGrid itself.

Code: Select all

on selectionChanged pHilitedIndex, pPrevHilitedIndex
    put pHilitedIndex into field "fieldHilitedIndex" -- perhaps you want to display the index somewhere
    put the dgDataOfIndex [ pHilitedIndex ] of me into tData
    put tData["Col 1"] into field "Some-Field-I-Wish-To-Contain-The-Value-Of-Col 1-For-Hilited-Row"
      -- note the "Col 1" is the column name, and not the column label
    put tData["Col 2"] into field "Some-Field-I-Wish-To-Contain-The-Value-Of-Col 2-For-Hilited-Row"
      -- note the "Col 2" is the column name, and not the column label
    put tData["Col 3"] into field "Some-Field-I-Wish-To-Contain-The-Value-Of-Col 3-For-Hilited-Row"
      -- note the "Col 3" is the column name, and not the column label
end selectionChanged

In situations where you wish to program functionality based on the currently hilited indices (multiple selected rows, and assuming that "Multiple Row Hilites" is checked in the Basic Properties section in the Property Inspector of your Data Grid). Create a button and place a handler such as this in the script of the button created.

Code: Select all

on mouseUp
   put the dgData of group "DataGrid 1" into tData1
   put the dgHilitedLines of group "DataGrid 1" into tIndexes
   
   repeat for each item tIndex in tIndexes
     -- place your functionality here
     -- you access values like so...
     put tData1[tIndex]["Col 1"] into fld "Some-Field-I-Wish-To-Contain-The-Value-Of-Col 1-For-Hilited-Row-With-tIndex"
       -- again "Col 1" is the column name, and not the column label
     put tData1[tIndex]["Col 2"] into fld "Some-Field-I-Wish-To-Contain-The-Value-Of-Col 2-For-Hilited-Row-With-tIndex"
       -- again "Col 2" is the column name, and not the column label
     put tData1[tIndex]["Col 3"] into fld "Some-Field-I-Wish-To-Contain-The-Value-Of-Col 3-For-Hilited-Row-With-tIndex"
       -- again "Col 3" is the column name, and not the column label
   end repeat
end mouseUp
I trust this will get you back on track.

Kind regards, Andrew

lohill
Posts: 770
Joined: Tue Dec 08, 2009 6:37 pm

Re: GetDataOfIndex for a DataGrid

Post by lohill » Sun Dec 27, 2009 1:22 am

Thanks for trying Andrew but I think you trying to help me with something way beyond what I trying to do. I'm just conducting a series of test of things as I read through the BlueMango documentantion. I literally have just one grid and one test button from which I am trying to get the feeling for what I can do with the DataGrid. I'm no where near an actual application yet. Here is an example of something that is similar to what I was trying to do in my original question that seems to work just fine:

Code: Select all

on mouseUp
  put the dgHilitedLines of group "DataGrid 1" into theLine 
   put the dgDataOfLine[theLine] of group "DataGrid 1" into theDataA 
   answer theDataA["Ticker"]   --Where ticker is the name of a column from which I want the value
end mouseUp
I was trying for something similar in my question where I was using Index rater than Line. The documentation implies that it should work but it didn't I was hoping that someone would be able to debug my very simple code. Here are some of the other experiments that found to be successful to give you more of a flavor for what I am doing.

Code: Select all

on mouseup
      put the dgHilitedLines of group "DataGrid 1" into theLine 
      put the dgDataOfLine[theLine] of group "DataGrid 1" into theDataA 
      answer theDataA["Ticker"] && theDataA["Current Price"]
end mouseup

Code: Select all

on mouseUp
     put the dgData of group "DataGrid 1" into pBigArray
       put pBigArray[3] into pLittleArray
      answer pLittleArray["Ticker"] 
end mouseUp

Code: Select all

on mouseUp
     put the dgDataOfLine[3] of group "DataGrid 1" into myLineArray
      put "$18.99" into pPrice
      put pPrice into myLineArray["Current Price"]
      set the dgDataofLine[3] of group "DataGrid 1" to myLineArray
end mouseUp
Best regards,
Larry

lohill
Posts: 770
Joined: Tue Dec 08, 2009 6:37 pm

Re: GetDataOfIndex for a DataGrid

Post by lohill » Sun Dec 27, 2009 1:39 am

Also Andrew - I meant to ask in my last post. Where do pHilitedIndex and pPrevHilitedIndex come from in your selectionChanged event? Who sends them? Are they passed from handlers that are built into the DataGrid?

Thanks again. I hope you had a nice Holiday,
Larry

dickey
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 118
Joined: Wed Apr 08, 2009 11:54 pm

Re: GetDataOfIndex for a DataGrid

Post by dickey » Sun Dec 27, 2009 2:29 am

Larry, I must say you can be particularly dismissive when people spend their valuable time to help you.

My code applies equally to a single Data Grid and single button example as it does when you have many Data Grids. Please take the time to implement suggestions before dismissing them so quickly.

OK back your original code.

1. your main issue is that you first need to decide whether you are implementing functionality for a single hilited index (row) or for multiple hilited indices (rows). If you are implementing code for multiple hilited indices (rows) you need some looping structure to process multiple items within the dgHilitedIndexes or dgHilitedLines arrays. If you are interested in a single hilited row only, use dgHilitedIndex or dgHilitedLine.

2. addressing your original code:

your code...

Code: Select all

on mouseUp
   put the dgHilitedIndexes of group "DataGrid 1" into theIndex 
   answer theIndex
   put GetDataOfIndex(theIndex, "Ticker") into theColValue     -- the heading for column 1 is "Ticker"
   answer theColValue
end mouseup
recommended code...

Code: Select all

on mouseUp
  put the dgData of group "DataGrid 1" into tData -- obtain the data grid's data
  put the dgHilitedIndex of group "DataGrid 1" into tIndex -- obtain the selected (hilited) index
  -- but "dgHilitedLine of group "DataGrid 1" into tIndex" works equally well.
  put tData[tIndex]["Col 1"] into tInfo -- obtain the data for the selected column "Col 1", for the selected index, for the selected Data Grid and place it in a variable
  answer info tinfo -- display the contents of that variable as a dialog
end mouseUp
This code has been tested thousands of times, and works well, as does my first and more detailed solution.

...and as I stressed in my first response, refer to the column name and not the column label in code. In your case check that "Ticker" is indeed the column name and not the column label.

I trust you find my post helpful this time around.

Kind regards, Andrew

dickey
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 118
Joined: Wed Apr 08, 2009 11:54 pm

Re: GetDataOfIndex for a DataGrid

Post by dickey » Sun Dec 27, 2009 3:17 am

Hello Larry,
Also Andrew - I meant to ask in my last post. Where do pHilitedIndex and pPrevHilitedIndex come from in your selectionChanged event? Who sends them? Are they passed from handlers that are built into the DataGrid?

Thanks again. I hope you had a nice Holiday,
Larry
Yes, provided by the Data Grid.

Kind regards, Andrew

dickey
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 118
Joined: Wed Apr 08, 2009 11:54 pm

Re: GetDataOfIndex for a DataGrid

Post by dickey » Sun Dec 27, 2009 5:53 am

Larry, alternatively...

You can access the dataset of the Data Grid using the selected index in conjunction with the dgDataOfIndex syntax, like so:

Code: Select all

on mouseUp
     -- obtain the selected (hilited) index
     put the dgHilitedIndex of group "DataGrid 1" into tIndex   
     -- put the array of values for the row whose index is hilited into variable tInfo
     put the dgDataOfIndex[tIndex] of group "DataGrid 1" into tInfo
     answer info tInfo["Col 1"] -- display a dialog with the value of  column "Col 1"
     answer info tInfo["Col 2"] -- display a dialog with the value of column "Col 2"
     answer info tInfo["Col 3"] -- display a dialog with the value of column "Col 3"
     -- do whatever processing you like with the values returned from dgData...
end mouseUp
Larry, you should be pretty well covered now, but let me know if you are still stuck.

- Kind regards, Andrew

lohill
Posts: 770
Joined: Tue Dec 08, 2009 6:37 pm

Re: GetDataOfIndex for a DataGrid

Post by lohill » Sun Dec 27, 2009 4:37 pm

Thanks Andrew. You are being very helpful and I apologize for sounding 'dismissive'. That was not my intention. I will try in the future to be more polite.

Larry

dickey
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 118
Joined: Wed Apr 08, 2009 11:54 pm

Re: GetDataOfIndex for a DataGrid

Post by dickey » Sun Dec 27, 2009 11:37 pm

Larry,

Thank you for your reply.

In the end were you able to retrieve the values you wanted from the Data Grid?

In reference to your other post http://forums.runrev.com/phpBB2/viewtop ... f=8&t=4588, the approach of creating a grid as a background, then creating multiple cards with that background, and displaying different data in each grid is sound. I have used that approach extensively over the last two months.

Kind regards, Andrew

lohill
Posts: 770
Joined: Tue Dec 08, 2009 6:37 pm

Re: GetDataOfIndex for a DataGrid

Post by lohill » Tue Dec 29, 2009 5:38 pm

Andrew,
In the end were you able to retrieve the values you wanted from the Data Grid?
Yes - thanks to your help.

I am still having trouble with the DataGrid in the background approach and have added more to that thread. It only it seems to work if I copy and paste to get the grid working on a new card.

Regards,
Larry

dickey
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 118
Joined: Wed Apr 08, 2009 11:54 pm

Re: GetDataOfIndex for a DataGrid

Post by dickey » Wed Dec 30, 2009 7:57 am

Larry, No promises, but if I get time later tonight I will take another look at this issue, and try and make a demo stack for you.

Kind regards, Andrew

Post Reply