LiveCode LessonsLiveCode Data Grid Working With Data Grids (Forms & Tables)How Do I Get Data Associated With a Row or Column?

How Do I Get Data Associated With a Row or Column?

This lesson will show you how to get the array of data associated with a row in a Data Grid form as well as how to get the data associated with a row's column in a Data Grid table.

To get the array of data from a Data Grid you can use one of the following properties:

• the dgDataOfIndex

• the dgDataOfLine

Both properties return the array associated with a row in the Data Grid, they just allow you to target the row differently. dgDataOfLine allows you to target the row in the order it appears visually in the Data Grid. dgDataOfIndex allows you to target a row based on the internal index number that the Data Grid uses to identify the row. This value does not change, even if you change the sort order for the Data Grid.

If you just need a particular column from a row then you can use one of the following functions:

GetDataOfIndex()

GetDataOfLine()

Both of these functions return the value associated with a particular key, or column, for a row.

Let's look at some examples of how to use these properties and functions.

Getting Data Associated with the Selected Row

Getting Data Associated with the Selected Row

If you want to get the data associated with the selected row use the dgHilitedLines property. The value of that property can be used in conjunction with the dgDataOfLine property. Here is an example of a script that could be placed in a button. The Data Grid group script is NOT in the message path for this example:

on mouseUp pBtnNum

if pBtnNum is 1 then

put the dgHilitedLines of group "DataGrid 1" into theLine

put the dgDataOfLine[theLine] of group "DataGrid 1" into theDataA

## theDataA is now an array variable.

## In the case of the Data Grid pictured above the keys of the array are id, FirstName, LastName and Title.

## This answer dialog will display: Dusty Bottoms

answer theDataA["FirstName"] && theDataA["LastName"]

end if

end mouseUp

Now let's assume that you only want to get the "id" value for the selected row without fetching the entire array. You can use GetDataOfLine to accomplish this. You need to keep in mind that GetDataOfLine (and GetDataOfIndex) is a function defined for a Data Grid. That means that the Data Grid group MUST be in the message path. If the Data Grid group is not in the message path you would need to use the call command.

To illustrate how to use GetDataOfLine let's look at how you could define your own custom property for a Data Grid. The following code defines a uSelectedID custom property that returns the id of the selected row. This code would be placed in the Data Grid group script.

getProp uSelectedID

put the dgHilitedLines of me into theLine

return GetDataOfLine(theLine, "id")

end uSelectedID

You can now access this custom property from any script in your application:

put the uSelectedID of group "DataGrid" into theSelectedID

Getting Data When The Selection Changes

When the user makes a new selection in a Data Grid the selectionChanged message is sent to the Data Grid. The following selectionChanged message could appear in the script of your Data Grid group:

-- example that would be placed in the data grid script.

on selectionChanged pHilitedIndex, pPrevHilitedIndex

put the dgDataOfIndex [ pHilitedIndex ] of me into theDataA

## Now call handler in card script that loads info for selected person.

uiViewRecordOfID theDataA["id"]

end selectionChanged

Getting Data In A Row Behavior

Getting Data In A Row Behavior

If you want to get the data in a row behavior script you can use the dgIndex or dgLine custom properties of the row control. Here is an example that could be used in a Data Grid form behavior script:

==========

Data Grid Form Example: Row Behavior

==========

on mouseUp pMouseBtnNum

if pMouseBtnNum is 1 then

## the dgIndex is a custom property of this row.

## the dgControl is a custom property of the data grid itself.

put the dgDataOfIndex[ the dgIndex of me] of the dgControl of me into theDataA

uiViewRecordOfID theDataA["id"]

end if

end mouseUp

If you wanted to move this same mouseUp code into the Data Grid script itself then you would change the 'me' references to 'target' and add a check to ensure that the user clicked on a row. This example also uses GetDataOfIndex rather than dgDataOfIndex to show an alternative.

==========

Data Grid Form Example: Data Grid Script

==========

on mouseUp pMouseBtnNum

if pMouseBtnNum is 1 then

## the dgIndex is a custom property of the row.

put the dgIndex of the target into theIndexThatWasClickedOn

if theIndexThatWasClickedOn is not empty then

put GetDataOfIndex(theIndexThatWasClickedOn, "id") into theID

uiViewRecordOfID theID

end if

end if

end mouseUp

Getting Data In A Column Behavior

Getting Data In A Column Behavior

If you want to get the data in a column behavior script you can use the dgIndex or dgLine and dgColumn custom properties of the column control. Here is an example that could be used in a Data Grid column behavior script:

==========

Data Grid Table Example: Column Behavior

==========

on mouseUp pMouseBtnNum

if pMouseBtnNum is 1 then

## the dgIndex is a custom property of the row.

## the dgColumn is a custom property of the column.

put GetDataOfIndex(the dgIndex of me, the dgColumn of me) into theColumnValue

## Do something with column value..

end if

end mouseUp

If you wanted to move this same mouseUp code into the Data Grid script itself then you would change the 'me' references to 'target' and add a check to ensure that the user clicked on a column:

==========

Data Grid Table Example: Data Grid Script

==========

on mouseUp pMouseBtnNum

if pMouseBtnNum is 1 then

## the dgIndex is a custom property of the row.

## the dgColumn is a custom property of the column.

put the dgColumn of the target into theColumn

if theColumn is not empty then ## User clicked on a column

put GetDataOfIndex(the dgIndex of the target, theColumn) into theColumnValue

## Do something with column value..

end if

end if

end mouseUp

0 Comments

Add your comment

E-Mail me when someone replies to this comment