1 Reply Latest reply on Nov 15, 2007 4:35 PM by ntsiii

    DataDrid Woes

    JustinFitzsimmons
      Hi,

      I've just started working on flex recently, and I'm running into trouble trying to do more advanced things with DataGrids. First off, I'm trying to create a datagrid with 1+n number of columns. Currently I'm doing something like this:

      <mx:DataGrid id="test" dataProvider="{testCollection}" >
      <mx:columns>
      <mx:DataGridColumn headerText="Category" dataField="categories" />
      </mx:columns>
      </mx:DataGrid>

      Is there a format of either XML or ArrayCollection (or even some other data type) that the DataGrid accepts as a dataProvider that will automatically be able to populate fields and column headers for me? What is that format? For example, I provided the following ArrayCollection as the provider for the above code:

      {"categories", ["Cat 1","Cat 2"]}

      But this only results in one row, with the strings in the array joined by a comma. I can't think of what other format it could possibly expect, and I am completely unable to find any documentation of any kind on the matter.

      As for dynamic numbers of columns, I was thinking of doing something like this, based on XML that I'm making an HTTP request for. Again however, it would be a lot easier if I was able to just format the XML in a way that could automatically populate the headers and associate the item's vertical position in the column with the proper category/row id. Any tips?

      var cols: Array = test.columns;
      //for loop that iterates through number of columns
      for( ... )
      {
      var col : DataGridColumn = new DataGridColumn();
      // set up the properties of the column
      ...
      cols.push(col)
      }
      ... etc.
        • 1. Re: DataDrid Woes
          ntsiii Level 3
          If you do not specify and DataGridColumns, then the DG will generate them dynamically, based on the property names of each item.

          The DG and other list-based controls expect a collection or list or array of items. Each item produces a row, and the column values are property values of each item object.

          Array is not good if you plan to change the dataprovider programatically, because it does not dispatch the events necessary to sync the ui. But for a simple example:
          var aDP:Array = [ {Cat1:"value1"},{Cat1:"value2"}, {Cat1:"value3"} ];

          would automatically produe a datagrid:
          Cat1
          value1
          value2.
          value2

          But it is rare that this automatic formatting meets the requirements. If you do not want to pre-define yu columns, then you ca build the entire DataGrid dynamically.

          There is a simple example here
          http://www.cflex.net/showFileDetails.cfm?ObjectID=552

          Tracy