3 Replies Latest reply on Apr 19, 2009 9:39 AM by HyderAlamgir

    DataGrid with Array as dataProvider?

      Hi,

       

      I have an Array of objects to be used as a datasource for a datagrid. How do I make it work? Any help appreciated.

       

      Thanks,

       

      Paul

       

       

      // the array

      [Bindable]

      public var arOrderItems:Array;

      // an array of these

      public class orderItem

      {

      // make 'em public to keep it simple

      public var qty:int;

      public var code:String;

       

       

      //---------------------------------------

      public function orderItem()

      {

      }

      //---------------------------------------

      public function setOrderItem(q:int, c:String):void

      {

      qty = q;

      code = c;

      }

      //---------------------------------------

      }

       

      // the datagrid points to the Array 'arOrderItems'

      <mx:DataGrid x="287" y="10" height="246" width="307" dataProvider="{Application.application.arOrderItems}">

      <mx:columns>

      <mx:DataGridColumn headerText="Qty" dataField="orderItem.qty" width="50"/>

      <mx:DataGridColumn headerText="Item" dataField="orderItem.code"/>

      </mx:columns>

      </mx:DataGrid>

       

        • 1. Re: DataGrid with Array as dataProvider?
          _Natasha_ Level 4

          // the array

           

          [Bindable]

          public var arOrderItems:Array;

          // an array of these

          public class orderItem

          {

          // make 'em public to keep it simple

          public var qty:int;

          public var code:String;

           

           

          //---------------------------------------

          public function orderItem()

          {

          }

          //---------------------------------------

          public function setOrderItem(q:int, c:String):void

          {

          qty = q;

          code = c;

          }

          //---------------------------------------

          }

           

          // the datagrid points to the Array 'arOrderItems'

           

          <mx:DataGrid x="287" y="10" height="246" width="307" dataProvider="{arOrderItems}">

          <mx:columns>

          <mx:DataGridColumn headerText="Qty" dataField="qty" width="50"/>

          <mx:DataGridColumn headerText="Item" dataField="code"/>

          </mx:columns>

          </mx:DataGrid>

          • 2. Re: DataGrid with Array as dataProvider?
            Gregory Lafrance Level 6

            Usually it is best to use an ArrayCollection as the dataProvider so you can wrap your Array in an ArrayCollection like this:

             

            private var myAC:ArrayCollection = new ArrayCollection(arOrderItems);

            • 3. Re: DataGrid with Array as dataProvider?
              HyderAlamgir

              The reason ArrayCollection serves you better than an Array is because ArrayColection dispatches a CollectionChange event when changes are make to it. That is also the reason Arrays are not bindable while ArrayCollections are.