tags:

views:

4827

answers:

2

Hello all,

I need to convert a datagrid table in Adobe Flex to an ArrayCollection. I was expecting to be able to loop through each row of a datagrid and write that to the Array collection, but the only method for accessing data in the datagrid that I can find is SelectedItem, which doesn't help me.

Obviously one could just copy the dataProvider for the datagrid, but my datagrid is editable and I need to store the state of the datagrid at any one time into a database. Can anyone recommend a method of doing this?

Much Appreciated

-Matt

+2  A: 

You can do it by iterating over the fields in the columns, but you should be using the data provider. If the only place the state of your data resides is in a transient user interface control then you have to get it back into the domain objects and serialise them. Presumably each row in the datagrid is an object of some sort, in which case I would re-cast your problem as how to keep the edits in the datagrid and the domain objects in sync. If you crack that then there's no need to iterate over the datagrid.

The easiest way to solve the sync problem is to watch events on your editing renderers in the datagrid. When the grid contents change you automatically update the domain object.

What are your in place editors in the grid?

Simon
+3  A: 

If your DataGrid is:

<mx:DataGrid id="someDG" dataProvider="{this.provider}" />

Then check if this.provider is Array or ArrayCollection. If it is ArrayCollection then access it simply by:

var gotIt:ArrayCollection = this.someDG.dataProvider as ArrayCollection;

if it is an Array, then:

var gotIt:ArrayCollection = new ArrayCollection(this.someDG.dataProvider as Array);

Hope this helps.

radekg
If it's bound to an HTTPService then chances are the dataProvider is an XMLListCollection. But you can still retrieve the values as above and just use XMLListCollection instead of Array or ArrayCollection.
paul_sns