Adapt Flex Datagrid height to content

By | 9 March, 2011

This is a fix for a recurring problem. When we want to make the datagrid’s height to cover its content (dataprovider).

<mx:DataGrid xmlns:mx=”http://www.adobe.com/2006/mxml”
variableRowHeight=”true” verticalScrollPolicy=”off” wordWrap=”true” rowCount=”{dataProvider.length}”
dataChange=”resize()” columnStretch=”resize()” resize=”resize()” >

<mx:Script>
<![CDATA[

//This is a work around for the poorly designed wordWrap and variableRowHeight features of the DataGrid
private function resize():void
{
if (dataProvider)
{
var h:Number = measureHeightOfItems( -1, dataProvider.length + 1);
if (h > maxHeight && maxHeight != 0)
{
verticalScrollPolicy = “on”;
h = maxHeight;
}else{
verticalScrollPolicy = “off”;
this.height = h;
}
}

]]>
</mx:Script>
</mx:DataGrid>

Got it from here:

<a href=”http://www.actionscript.org/forums/showthread.php3?t=220962″ target=”_blank” >http://www.actionscript.org/forums/showthread.php3?t=220962</a>