2 Replies Latest reply on Jul 29, 2009 9:34 AM by moutfeh5

    how to bind XML to dataGrid




      I am retrieving the content of an xml file through a .NET web service, the webMethod return the content.


      I need to bind the result to a dataGrid, i tried to convert the result to Array,






      var xmlListCollection:XMLListCollection = new XMLListCollection(ws_result.children().children().children());




      var resultArray:Array = xmlListCollection.toArray();

      dataGrid.dataprovider = resultArray;


      it works, but the problem is: that the header of each row is prefixed with "@" symbol and the columns are sorted alphabetically.


      N.B: I can't set the columns header previously, because each time the columns change.


      Is there any solution how to bind XML to a dataGrid


      Thank you







        • 1. Re: how to bind XML to dataGrid

          I think when you add data to a datagrid without defining the columns the column headers will be set to the actual property name. In your case, it looks like the property is an attribute causing the @ sign. What you probably want to do is dynamically create the columns after you get the data (since it sounds like you can't do it ahead of time)


          You can do something like this to add columns:

          var columns:Array = [];


          //you would probably do this in a loop to get the dynamic columns from the attributes

          var dataField:String = "@attributeToUseInColumn";//

          var dgc:DataGridColumn = new DataGridColumn(dataField); //you can set other properties on the column here too
          dgc.headerText = "Attribute To Use In Column";//you may want to use the dataField and trim off the @ sign and convert the camel case to normal text with spaces.



          //then add the columns to the grid

          datagrid.columns = columns;


          //then set the data provider of the grid

          var xmlListCollection:XMLListCollection = new XMLListCollection(ws_result.children().children().children());




          dataGrid.dataprovider = xmlListCollection;

          • 2. Re: how to bind XML to dataGrid
            moutfeh5 Level 1

            The problem is solved :)

            Thanks a lot