How Do I Export Data From A Data Grid?

This lesson will show you how to get data out of a data grid.

The Example Data Grid

The Example Data Grid

Here is what the data grid looks like that I will be exporting data from.

The Handler For Populating Data Grid

command uiPopulatePeople
   local theImageFolder, tDataA
   put "images/" into tImageFolder
   
   put "Lucky" into tDataA[1]["FirstName"]
   put "Day" into tDataA[1]["LastName"]
   put "Three Amigo" into tDataA[1]["Title"]
   put tImageFolder & "monkey.jpg" into tDataA[1]["Image URL"]
   
   put "Dusty" into tDataA[2]["FirstName"]
   put "Bottoms" into tDataA[2]["LastName"]
   put "Three Amigo" into tDataA[2]["Title"]
   put tImageFolder & "monkey.jpg" into tDataA[2]["Image URL"]
   
   put "Ned" into tDataA[3]["FirstName"]
   put "Nederlander" into tDataA[3]["LastName"]
   put "Three Amigo" into tDataA[3]["Title"]
   put tImageFolder & "monkey.jpg" into tDataA[3]["Image URL"]
   
   put "Jane" into tDataA[4]["FirstName"]
   put "Blue" into tDataA[4]["LastName"]
   put "Secret Agent" into tDataA[4]["Title"]
   put tImageFolder & "monkey.jpg" into tDataA[4]["Image URL"]
   
   put "Jefferson" into tDataA[5]["FirstName"]
   put "Blue" into tDataA[5]["LastName"]
   put "Secret Agent" into tDataA[5]["Title"]
   put tImageFolder & "monkey.jpg" into tDataA[5]["Image URL"]
   
   lock screen
   set the dgData of group "DataGrid 1" to theDataA
   
   ## Hilite first row
   set the dgHilitedLines of group "DataGrid 1" to 1
   unlock screen
end uiPopulatePeople

This is the code that was used to populate the data grid. This shows you the keys that each record has (FirstName, LastName, Title and Image URL).

Export Handler

command uiExportData
   ## Export data to XML
   local tDataA, tIndexes, tXML
   
   ## Get Data Grid Array
   put the dgData of group "DataGrid 1" into tDataA
   
   ## Get indexes in proper order
   put the dgIndexes of group "DataGrid 1" into tIndexes
   
   ## Prefix XML
   put "<people>" & cr into tXML
   
   ## Loop through data, putting into XML format
   repeat for each item tIndex in tIndexes
      put "<person>" & cr after tXML
      put "<first_name>" & tDataA[theIndex]["FirstName"] & "</first_name>" & cr after tXML
      put "<last_name>" & tDataA[theIndex]["LastName"] & "</last_name>" & cr after tXML
      put "<title>" & tDataA[theIndex]["Title"] & "</title>" & cr after tXML
      put "</person>" & cr after tXML
   end repeat
   
   ## Close root XML tag
   put "</people>" after tXML
   
   put tXML
end uiExportData

This is an example of how to get data out of a data grid. I begin by getting the dgData array and the dgIndexes. The indexes are a comma delimited list of the keys of the dgData in the proper order.

After you have the array and the ordered list of indexes you can loop through each record in the array. In this example I'm just wrapping the data in XML tags.

Example Output

Example Output

This is what the output for my example looks like in the message box.

2 Comments

ron bird

I dont want to export the whole grid, I just want to access a specific cell and export its contents. For example the contents of the cell of row 2 column 2.
How do I do that please?

Elanor Buchanan

Hi Ron

There are a couple of way you can do this. The first is to get the array associated with the whole table and look in the row and column you want e.g.

put the dgData of group "contacts" into tData
put tData[1]["last name"] into tCellData

Or just get the data associated with the line you are interested in and look up the column you want e.g.

put the dgDataOfLine[1] of group "contacts" into tData
put tData["last name"] into tCellData

If you don't know the name of the column you want, just its position you can use the columns property of the Data Grid e.g.

put line 2 of the dgProp["columns" ] of group "contacts" into tColumnName
put tData[tColumnName] into tCellData

You might also find this lesson on getting the data associated with a row or column helpful.

http://lessons.runrev.com/m/datagrid/l/7313-how-do-i-get-data-associated-with-a-row-or-column

Kind regards

Elanor

Add your comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.