1 Reply Latest reply on Aug 7, 2006 9:48 AM by peterent

    How to fill a datagrid

    Djunix
      Hello, my problem is that i don't know how can i fill a datagrid with the values of an array.

      for example:
      myArray[0]=value1;
      myArray[1]=value2;
      and so on...

      and now i want that one column of the datagrid has the values from myArray:

      col1
      --------------
      value1
      value2
        • 1. Re: How to fill a datagrid
          peterent Level 2
          In Flex, the data and the control (in this case a DataGrid) are separate. You don't assign data to cells or rows or columns of the DataGrid. Instead, you associate the data with the control using the control's dataProvider property. Then you manipulate the data and in doing that, causes the DataGrid to reflect the changes.

          [Bindable] private myArray:Array;

          <mx:DataGrid dataProvider="{myArray}">

          This will make the DataGrid use myArray as its dataProvider.

          However, an Array is not the best thing to use as a dataProvider because the Array does not know how to behave if you update it. More technically, the Array does not know how to dispatch data change events that the DataGrid will intercept.

          Instead, use an ArrayCollection:

          import mx.collections.ArrayCollection;

          [Bindable] private var myArray:ArrayCollection = new ArrayCollection();
          ...
          myArray.addItem( {Value:value1} );
          myArray.addItem( {Value:value2} );

          <mx:DataGrid dataProvider="{myArray}" ... />

          I used {Value:value1} to indicate a new Object (that's what { and } do here) with "Value" being the name of a field which DataGrid assumes to be the column's name unless you use a DataGridColumn to tell it otherwise.