views:

26

answers:

2

What I would like to do is simply add to a dataProvider, but when I do, I get an error.

Here's the code I'm trying to run...

dg.dataProvider.addItem(obj.ResultSet.Result[i]);

It's inside a for loop, using i as the integer.

It works great doing...

dg.dataProvider = obj.ResultSet.Result

But that won't work for me, because I need to add to the dataprovider more than once. I'm getting results in batches of 10, and I need to add each batch to the dataProvider when it's received.

I also tried to to do...

var dgDP:dataProvider = new dataProvider();

But for some reason Flex doesn't recognize it...

Any ideas on how I can make this happen?

+1  A: 

A dataProvider is a property which resides on many ListBased classes. It is not a data type. What is the data type of your dataProvider? IT can be XML, an array, an XMLListCollection, an ArrayCollection, an XMLList, or a generic object. [and I assume other data types are supported).

The 'how' you add something to your dataProvider depends entirely on the type of dataProvider you are using.

In Flex 4, the dataProvider objects must implement the IList interface, but in Flex 3 dataProviders are generic objects.

In your situation, since you already have the objects, I'd just loop over them and add them to an array or ArrayCollection and then use hat array as a dataProvider.

www.Flextras.com
_but in Flex __3__ dataProviders are generic objects_
Amarghosh
Good catch; I fixed my typo.
www.Flextras.com
__ __ +1 __ __ _ _
Amarghosh
A: 

You have to initialize the dataProvider.

<mx:DataGrid creationComplete="onDGCreate(event)"/>

Script:

public function onDGCreate(e:Event):void
{
    var dg:DataGrid = e.currentTarget as DataGrid;
    dg.dataProvider = new ArrayCollection();
    //or
    dg.dataProvider = new XMLListCollection();
}

Now this will work:

dg.dataProvider.addItem(obj.ResultSet.Result[i]);

When you assign something other than ArrayCollection and XMLListCollection to the dataProvider property, it will be converted to an ICollectionView object. The only implementer of this interface is the ListCollectionView class (base class of ArrayCollection and XMLListCollection) which has addItem and addItemAt methods.

Amarghosh