You need to populate something like an ArrayCollection and then set the
dataProvider of your DataGrid to that ArrayCollection. Here is a quick
and dirty example:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml";
layout="absolute"
     initialize="init()">
     <mx:Script>
         <![CDATA[
             import mx.controls.dataGridClasses.DataGridColumn;
             import mx.collections.ArrayCollection;

             [Bindable] private var dgCSVArrColl:ArrayCollection = new
ArrayCollection();

             private function init():void
             {
                 buildDG("Col1, Col2, Col3, Col4, Col5, Col6");
                 addDGRow("1, 2, 3, 4, 5, 6");
                 addDGRow("9, 8, 7, 6, 5, 4");
             }
             private function buildDG(sCSV:String):void
             {
                 var aCSV:Array = sCSV.split(",");
                 for(var i:int = 0; i < aCSV.length; i++)
                 {
                     var dgc:DataGridColumn = new
DataGridColumn(aCSV[i]);
                     var cols:Array = dgCSV.columns;
                     cols.push(dgc);
                     dgCSV.columns = cols;
                 }
             }
             private function addDGRow(sCSVData:String):void
             {
                 // Code for adding a row to the datagrid assuming
sCSVData is CSV data
                 // in the same order as sCSV above
                 var lCSV:Array = sCSVData.split(",");
                 var cols:Array = dgCSV.columns;
                 if(cols.length == lCSV.length)
                 {
                     var dataItem:Object = new Object();
                     for(var i:int = 0; i < cols.length; i++)
                     {
                         dataItem[(cols[i] as DataGridColumn).dataField]
= lCSV[i];
                     }
                     dgCSVArrColl.addItem(dataItem);
                 }
             }

         ]]>
     </mx:Script>
     <mx:DataGrid id="dgCSV" dataProvider="{dgCSVArrColl}"/>
</mx:Application>



--- In [email protected], "Josh Keller" <jeepi...@...> wrote:
>
> I'm new to Flex (just started looking at it 2 days ago) but I've been
working on this problem all day long. I'm trying to create and populate
a datagrid based on field names specified in a CSV list, with separate
data that is also going to be CSV but from a different source in the
same order if that makes sense.
>
> I've figured out how to make the first section of code below work as I
would expect, buildDG takes the CSV string and generates the columns and
headers in the datagrid like I want. The part I need help on is getting
the code in addDGRow to work, a function that will accept CSV data in
the same order as the headers and add that data as a new row in the
datagrid. I've been working on this all day and can't figure out how to
do this.
>
> Any help would be greatly appreciated. Thanks, Josh
>
>
> private function init():void
> {
> buildDG("Col1,Col2,Col3,Col4,Col5,Col6");
> }
> private function buildDG(sCSV:String):void
> {
> var aCSV:Array = sCSV.split(",");
> for ( var i:int = 0; i < aCSV.length; i++)
> {
> var dgc:DataGridColumn = new DataGridColumn(aCSV[i]);
> var cols:Array = dgCSV.columns;
> cols.push(dgc);
> dgCSV.columns = cols;
> }
> }
> private function addDGRow(sCSVData:String):void
> {
> // Code for adding a row to the datagrid assuming sCSVData is CSV data
in the same order as sCSV above
> }
> ...
> <mx:DataGrid id="dgCSV">
> </mx:DataGrid>
>

Reply via email to