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
   put "images/" into theImageFolder
   put "Lucky" into theDataA[1]["FirstName"]
   put "Day" into theDataA[1]["LastName"]
   put "Three Amigo" into theDataA[1]["Title"]
   put theImageFolder & "monkey.jpg" into theDataA[1]["Image URL"]
   put "Dusty" into theDataA[2]["FirstName"]
   put "Bottoms" into theDataA[2]["LastName"]
   put "Three Amigo" into theDataA[2]["Title"]
   put theImageFolder & "monkey.jpg" into theDataA[2]["Image URL"]
   put "Ned" into theDataA[3]["FirstName"]
   put "Nederlander" into theDataA[3]["LastName"]
   put "Three Amigo" into theDataA[3]["Title"]
   put theImageFolder & "monkey.jpg" into theDataA[3]["Image URL"]
   put "Jane" into theDataA[4]["FirstName"]
   put "Blue" into theDataA[4]["LastName"]
   put "Secret Agent" into theDataA[4]["Title"]
   put theImageFolder & "monkey.jpg" into theDataA[4]["Image URL"]
   put "Jefferson" into theDataA[5]["FirstName"]
   put "Blue" into theDataA[5]["LastName"]
   put "Secret Agent" into theDataA[5]["Title"]
   put theImageFolder & "monkey.jpg" into theDataA[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
   ## Get Data Grid Array
   put the dgData of group "DataGrid 1" into theDataA
   ## Get indexes in proper order
   put the dgIndexes of group "DataGrid 1" into theIndexes
   ## Prefix XML
   put "<people>" & cr into theXML
   ## Loop through data, putting into XML format
   repeat for each item theIndex in theIndexes
      put "<person>" & cr after theXML
      put "<first_name>" & theDataA[theIndex]["FirstName"] & "</first_name>" & cr after theXML
      put "<last_name>" & theDataA[theIndex]["LastName"] & "</last_name>" & cr after theXML
      put "<title>" & theDataA[theIndex]["Title"] & "</title>" & cr after theXML
      put "</person>" & cr after theXML
   end repeat
   ## Close root XML tag
   put "</people>" after theXML
   put theXML
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.


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.

Kind regards


Add your comment

E-Mail me when someone replies to this comment