You could set dataProvider of the data grid to null or an empty collection until ready to display your data.
By default, initialize null or an empty collection to the data grid. Then, when the desired combo box option is selected set the data grid data provider to valid data:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark">
<fx:Script>
<![CDATA[
import mx.collections.ArrayList;
import spark.events.IndexChangeEvent;
[Bindable]
public var data:ArrayList = new ArrayList([ "one", "two", "three" ]);
protected function combobox1_changeHandler(event:IndexChangeEvent):void
{
switch (comboBox.selectedItem)
{
case "Show data":
dataGrid.dataProvider = data;
break;
default:
dataGrid.dataProvider = null;
break;
}
}
]]>
</fx:Script>
<s:layout>
<s:VerticalLayout />
</s:layout>
<s:DataGrid id="dataGrid"
dataProvider="{null}" />
<s:ComboBox id="comboBox"
change="combobox1_changeHandler(event)">
<s:dataProvider>
<s:ArrayList>
<fx:String>Show data</fx:String>
<fx:String>Hide data</fx:String>
</s:ArrayList>
</s:dataProvider>
</s:ComboBox>
</s:Application>
Another approach would be to use Flex state system.
You could define two states to control visibility of data in the data grid. Then, set the data provider according to the current state:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
currentState="hideData">
<s:states>
<s:State name="showData" />
<s:State name="hideData" />
</s:states>
<fx:Script>
<![CDATA[
import mx.collections.ArrayList;
import spark.events.IndexChangeEvent;
[Bindable]
public var data:ArrayList = new ArrayList([ "one", "two", "three" ]);
protected function combobox1_changeHandler(event:IndexChangeEvent):void
{
switch (comboBox.selectedItem)
{
case "Show data":
currentState = "showData";
break;
default:
currentState = "hideData";
break;
}
}
]]>
</fx:Script>
<s:layout>
<s:VerticalLayout />
</s:layout>
<s:DataGrid id="dataGrid"
dataProvider.hideData="{null}"
dataProvider.showData="{data}" />
<s:ComboBox id="comboBox"
change="combobox1_changeHandler(event)">
<s:dataProvider>
<s:ArrayList>
<fx:String>Show data</fx:String>
<fx:String>Hide data</fx:String>
</s:ArrayList>
</s:dataProvider>
</s:ComboBox>
</s:Application>
dataProviderof the data grid to an empty collection. - Jason Sturges