Friday, July 2, 2010

Link a XML file to a DataGrid in Flex

This is a simple mxml file that will link a XML File containing some records to a DataGrid used in Flex. We first create a XMLLIstCollection to link the XML file to act as a collection od data for our datagrid. Be careful to use the id property of all the controls as they are of impotance in linking each other.THe sample code is mentioned here along with the XML records file.
You can add records to the XML file and it would directly reflect in the DataGrid Property.


<?xml version="1.0"?>
<!-- DataGrid control example. -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

<mx:XML id="testXML"
    source="C:\Documents and Settings\D Mondal\My Documents\Flex Builder 3\populate datagrid from xml\src\test.xml" />

<mx:XMLListCollection id="testXMLList" source="{testXML.record}" />


<mx:DataGrid id="testdatagrid"
    dataProvider="{testXMLList}" y="400" >
    <mx:columns>
        <mx:DataGridColumn id="namecol"
            dataField="name" />
        <mx:DataGridColumn id="agecol"
            dataField="age" />
        <mx:DataGridColumn id="sexcol"
            dataField="sex" />
    </mx:columns>
</mx:DataGrid>
       
</mx:Application> 

The XML FILE - LINK