If horizontalScrollPolicy="off" (default), the DG resizes columns to fit
the available space so you have less control over their size, and as you
change one column's size, the others respond.  Temporarily setting
resizable=false often helps, but it might be better to reset the entire
column set at once with new columns with the right size.  Make sure they
fit perfectly otherwise they'll get resized.

 

________________________________

From: [email protected] [mailto:[EMAIL PROTECTED] On
Behalf Of brileach
Sent: Wednesday, January 16, 2008 12:51 PM
To: [email protected]
Subject: [flexcoders] DataGrid column resizing

 

I've been slamming my head against my desk for the last day trying to
figure out how to resize DataGrid columns and I'm hoping somebody can
help me out. I'm using Flex 2.0.1. Any help will be much appreciated!

The idea here is that onCreationComplete the DataGridColumns will
recreate themselves based off of a certain size, eventually the length
of the data inside of them. I've gotten this to work with every
attribute except width. (labelFunctions, itemRenderers, etc.)

<mx:Script>
<![CDATA[

private var gridData:Array = [
{ symbol: "ADBE", name: "Adobe Systems Inc.", price: 49.95 },
{ symbol: "MACR", name: "Macromedia Inc.", price: 39.95 },
{ symbol: "MSFT", name: "Microsoft Corp.", price: 25.95 },
{ symbol: "IBM", name: "IBM Corp.", price: 42.55 }
];

public function resizeGridColumns(gridId:Object):void {
var oldColumns:Array = gridId.columns;
var numberOfColumns:int = oldColumns.length;
var newColumns:Array = new Array();
for(var i:int = 0; i < numberOfColumns; i++){
var oldColumn:DataGridColumn = (oldColumns[i] as DataGridColumn);
var newColumn:DataGridColumn = new DataGridColumn;
newColumn.dataField = oldColumn.dataField;
newColumn.headerText = oldColumn.headerText+"-dynamic";
newColumn.width = 50;
newColumns.push(newColumn);
}
gridId.columns = newColumns;
}

]]>
</mx:Script>

<mx:DataGrid id="grid" 
initialize="grid.dataProvider = gridData" 
creationComplete="resizeGridColumns(grid)"
width="500">
<mx:columns>
<mx:DataGridColumn headerText="Name" dataField="name" />
<mx:DataGridColumn headerText="Symbol" dataField="symbol" />
<mx:DataGridColumn headerText="Price" dataField="price" />
</mx:columns> 
</mx:DataGrid>

I just can't figure out how to set a column width on the fly. 

Thanks!

--Brian Leach, [EMAIL PROTECTED] <mailto:brileach%40yahoo.com> 

 

Reply via email to