selectionChanged for datagrids

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Paul D
Posts: 116
Joined: Mon May 21, 2007 6:58 pm

selectionChanged for datagrids

Post by Paul D » Fri Aug 07, 2009 9:14 pm

I found an example stack that uses this method to populate the datagrid...

put revDatabaseColumnNames(sCursorID) into sRecordFields
put revNumberOfRecords(sCursorID) into sRecordCount
set the dgNumberOfRecords of group "datagrid 1" to sRecordCount

command GetDataForLine pLine, @pOutData
revMoveToRecord sCursorID, pLine - 1
put ConvertCurrentRowToArray() into pOutData
end GetDataForLine

function ConvertCurrentRowToArray
local theArray
repeat for each item theField in sRecordFields
put revDatabaseColumnNamed(sCursorID,theField) into theArray[theField]
end repeat
return theArray
end ConvertCurrentRowToArray


selectionChanged code...

on selectionChanged pHilitedIndex, pPrevHilitedIndex
put the dgDataOfIndex [ pHilitedIndex ] of me into theDataA
end selectionChanged


Result...
Image


If I use this method to populate the datagrid...

put revDatabaseColumnNames(sConnID, table) into theHeaders
put revDataFromQuery(,,sConnID,"SELECT * from table;") into theData
put 0 into x
repeat for each line dataLine in theData
add 1 to x
put 0 into y
repeat for each item headerTitle in theHeaders
add 1 to y
put item y of dataLine into arrayData[x][headerTitle]
end repeat
end repeat
set the dgData of group "datagrid 1" to arrayData



selectionChanged code...

on selectionChanged pHilitedIndex, pPrevHilitedIndex
put the dgDataOfIndex [ pHilitedIndex ] of me into theDataA
end selectionChanged


Result...
Image

Both ways of populating the datagrid show the correct data but why does selectionChanged work for one but not the other?

trevordevore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1005
Joined: Sat Apr 08, 2006 3:06 pm
Contact:

Post by trevordevore » Sat Aug 08, 2009 2:31 am

The first method uses a special feature of the data grid that is used for displaying really large amounts of data. Basically the data grid doesn't store any data internally but rather requests data (by calling GetDataForLine) for any data it needs to display. You can learn more about it here:

Displaying Large Amounts of Data

In the example you posted the data comes from a database. Rather than querying the data grid for data you would need to query the database. If you look at the sample stack associated with the above lesson the behavior for the data grid shows how you can store the database id in FillInData and then retrieving it in selectionChanged.
Trevor DeVore
ScreenSteps - https://www.screensteps.com

LiveCode Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode
LiveCode Builder Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode-builder

Paul D
Posts: 116
Joined: Mon May 21, 2007 6:58 pm

Post by Paul D » Mon Aug 10, 2009 1:03 pm

sounds good. thanks again.

Post Reply