The AdvancedDataGrid control does not clear the openNodes property if you replace the data provider with a new data provider. Because the openNodes property contains references to objects in the old data provider, the objects cannot be removed from memory by the garbage collection mechanism
When using the AdvancedDataGrid control, you can update the control's data provider at runtime. There are several different ways to update the data provider. For example, you can add items, remove items, or reset the data provider. A reset typically means that you replaced the entire contents of the data provider with new content.
Internally, the AdvancedDataGrid control represents hierarchical and grouped data by using the IHierarchicalCollectionView interface. The IHierarchicalCollectionView interface defines the openNodes property. This property contains an Array of objects that represent the nodes of the data provider that are currently open in the AdvancedDataGrid. In most cases, you can ignore the openNodes property when you update the data provider.
However, if you replace the data provider with a new data provider, the AdvancedDataGrid control does not clear the openNodes property. Because the openNodes property contains references to objects in the old data provider, the objects cannot be removed from memory by the garbage collection mechanism. To ensure that the old data provider can be removed from memory, use an event handler to clear the openNodes property when you replace the data provider.
When you modify the data provider, the data provider dispatches a collectionChange event, which an object of type mx.events represents.CollectionEvent. In the event handler for the collectionChange event, you can use the CollectionEvent.type property to determine the type of modification. If the type is RESET, clear the openNodes property as shown below:
<?xml version="1.0"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:s="library://ns.adobe.com/flex/spark">
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.collections.IHierarchicalCollectionView;
import mx.events.CollectionEvent;
import mx.events.CollectionEventKind;
// Define the data provider
[Bindable]
private var results:ArrayCollection;
// Refresh the data provider.
private function refresh():void{
// Add the removeOpenNodes event handler to the dataProvider.
if (adg.dataProvider is IHierarchicalCollectionView)
IHierarchicalCollectionView(adg.dataProvider).addEventListener(CollectionEvent.COLLECTION_CHANGE, removeOpenNodes);
// generateMyData() returns data for the ADG.
results = generateMyData();
myGC.refresh();
// Remove the removeOpenNodes event handler when done updating the dataProvider.
if (adg.dataProvider is IHierarchicalCollectionView)
IHierarchicalCollectionView(adg.dataProvider).removeEventListener(CollectionEvent.COLLECTION_CHANGE, removeOpenNodes);
}
// Handle the colectionChange event.
private function removeOpenNodes(event:CollectionEvent):void {
if(event.kind == CollectionEventKind.RESET)
IHierarchicalCollectionView(adg.dataProvider).openNodes = {};
}
// Generate the data for the dataProvider.
private function generateMyData():ArrayCollection {
var newData:ArrayCollection = new ArrayCollection();
// Returns a new ArrayCollection.
return newData;
}
]]>
</fx:Script>
<mx:AdvancedDataGrid id="adg" creationComplete="refresh()">
<mx:dataProvider>
<mx:GroupingCollection2 source="{results}" id="myGC">
<mx:grouping>
...
</mx:grouping>
</mx:GroupingCollection2>
</mx:dataProvider>
<mx:columns>
<mx:AdvancedDataGridColumn dataField="Name"/>
<mx:AdvancedDataGridColumn dataField="Age"/>
<mx:AdvancedDataGridColumn dataField="Phone"/>
</mx:columns>
</mx:AdvancedDataGrid>
<s:Button label="Refresh" click="refresh()"/>
</s:Application>
In this example, you update the dataProvider by replacing the entire ArrayCollection on a refresh. Therefore, attach the event handler for the collectionChange event to the dataProvider of the AdvancedDataGrid.
Alternatively, you can update the source property of the ArrayCollection to replace just the data. In that case, attach the event handler for the collectionChange event to the results ArrayCollection, as shown below:
results.addEventListener(CollectionEvent.COLLECTION_CHANGE, removeOpenNodes);

