5 Replies Latest reply on Jun 24, 2010 10:05 AM by Flex harUI

    Binding xml to datagrid


      Hi all.


      I am new to Flex.


      I am trying to bind testxml variable to datagrid.




      - <dataObj>
      <var n="_cmd" t="s">RowList</var>
      - <obj o="rows" t="a">
      <var n="338" t="s">Test1,322,3,20,2,100,200,Challenge,,,40000,9,,,Public,,0,normal,0,,</var>
      <var n="339" t="s">Test2,323,0,20,0,100,200,Challenge,,,40000,9,,,Public,,0,normal,0,,</var>
      <var n="332" t="s">Test3,345,0,20,0,100,200,Challenge,,,40000,9,,,Public,,0,normal,0,,</var>


          <mx:DataGrid width="786" id="odalar" dataProvider="{testXML.obj}">
                  <mx:DataGridColumn headerText="Column 1" dataField="var"/>



      this puts all xml data to one datagrid row.

      Could you please help?


        • 1. Re: Binding xml to datagrid
          BhaskerChari Level 4

          Hi Woka_Junior,


          Check out the below code:


          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init();">
             import mx.collections.ArrayCollection;
             import mx.rpc.xml.SimpleXMLDecoder;
             [Bindable]private var dataProvider:ArrayCollection = new ArrayCollection();
             private function init():void
              var xml:XML = testXML;
              var xmlDoc:XMLDocument = new XMLDocument(xml);
              var decoder:SimpleXMLDecoder = new SimpleXMLDecoder(true);
              var xmlObj:Object = decoder.decodeXML(xmlDoc);
              var resultObj:Object = xmlObj.dataObj;
              if(resultObj.obj.var1 is ArrayCollection)
               dataProvider = resultObj.obj.var1 as ArrayCollection;
              else if(resultObj.obj.var1 is Object)

          <mx:XML xmlns="" id="testXML">
            <var n="_cmd" t="s">RowList</var>
            <obj o="rows" t="a">
            <var1 n="338" t="s">Test1,322,3,20,2,100,200,Challenge,,,40000,9,,,Public,,0,normal,0,,</var1>
            <var1 n="339" t="s">Test2,323,0,20,0,100,200,Challenge,,,40000,9,,,Public,,0,normal,0,,</var1>
            <var1 n="332" t="s">Test3,345,0,20,0,100,200,Challenge,,,40000,9,,,Public,,0,normal,0,,</var1>


              <mx:DataGrid width="786" id="odalar" dataProvider="{dataProvider}">
                      <mx:DataGridColumn headerText="n" dataField="n"/>
                      <mx:DataGridColumn headerText="t" dataField="t"/>
                      <mx:DataGridColumn headerText="value" dataField="value"/>



          Note: I have changed var to var1 in your xml file just to differentiate it from predefined var keyword in flex(Used to declare a variable in Flex).


          If this post answers your question or helps, please kindly mark it as such.


          Bhasker Chari

          • 2. Re: Binding xml to datagrid
            Woka_Junior Level 1

            Thanks a lot BhaskerChar.


            One more question please.


            If I want to write the third row's "n" argument (which 332) to a textbox, what would be the syntax?


            Thanks in advance.






            • 3. Re: Binding xml to datagrid
              BhaskerChari Level 4



              You can access the value using the datagrid's dataprovider or you can also use the datagrid's seectedItem property if you want the value when an item is selected in a grid.


              Using dataprovider you can do the following:


              var nValue:String = String(dataProvider.getItem(2).n);




              Bhasker Chari

              1 person found this helpful
              • 4. Re: Binding xml to datagrid
                Woka_Junior Level 1

                Thanks a lot.

                • 5. Re: Binding xml to datagrid
                  Flex harUI Adobe Employee

                  {testXML.obj.var} will get you three rows.  Because your data is not in

                  attributes or subnodes you will prbably need labelFunctinos or custom