The following example shows how you can toggle word wrapping on a Flex DataGrid control’s DataGridColumn by setting the headerWordWrap property.
Full code after the jump.
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/09/14/toggling-word-wrap-on-a-datagrid-column-header-in-flex/ -->
<mx:Application name="DataGridColumn_headerWordWrap_test"
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="top"
backgroundColor="white">
<mx:XML id="dp" source="data/products.xml" />
<mx:ApplicationControlBar dock="true">
<mx:Form styleName="plain">
<mx:FormItem label="headerWordWrap:">
<mx:CheckBox id="checkBox" />
</mx:FormItem>
<mx:FormItem label="rowCount:">
<mx:HSlider id="slider"
minimum="2"
maximum="10"
value="6"
snapInterval="1"
tickInterval="1"
liveDragging="true"
showTrackHighlight="true" />
</mx:FormItem>
</mx:Form>
</mx:ApplicationControlBar>
<mx:DataGrid id="dataGrid"
dataProvider="{dp.product}"
rowCount="{slider.value}"
verticalScrollPolicy="on"
width="300">
<mx:columns>
<mx:DataGridColumn id="dataGridColumn1"
dataField="@name"
headerText="This is a column with a long title:"
headerWordWrap="{checkBox.selected}"
minWidth="80" />
<mx:DataGridColumn id="dataGridColumn2"
dataField="@price"
headerText="Price:"
headerWordWrap="false"
minWidth="20" />
</mx:columns>
</mx:DataGrid>
</mx:Application>
View source is enabled in the following example.
You can also set the headerWordWrap property using ActionScript, as seen in the following example:
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/09/14/toggling-word-wrap-on-a-datagrid-column-header-in-flex/ -->
<mx:Application name="DataGridColumn_headerWordWrap_test"
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="top"
backgroundColor="white">
<mx:Script>
<![CDATA[
private function checkBox_change(evt:Event):void {
dataGridColumn1.headerWordWrap = checkBox.selected;
}
]]>
</mx:Script>
<mx:XML id="dp" source="data/products.xml" />
<mx:ApplicationControlBar dock="true">
<mx:Form styleName="plain">
<mx:FormItem label="headerWordWrap:">
<mx:CheckBox id="checkBox"
change="checkBox_change(event);" />
</mx:FormItem>
<mx:FormItem label="rowCount:">
<mx:HSlider id="slider"
minimum="2"
maximum="10"
value="6"
snapInterval="1"
tickInterval="1"
liveDragging="true"
showTrackHighlight="true" />
</mx:FormItem>
</mx:Form>
</mx:ApplicationControlBar>
<mx:DataGrid id="dataGrid"
dataProvider="{dp.product}"
rowCount="{slider.value}"
verticalScrollPolicy="on"
width="300">
<mx:columns>
<mx:DataGridColumn id="dataGridColumn1"
dataField="@name"
headerText="This is a column with a long title:"
minWidth="80" />
<mx:DataGridColumn id="dataGridColumn2"
dataField="@price"
headerText="Price:"
headerWordWrap="false"
minWidth="20" />
</mx:columns>
</mx:DataGrid>
</mx:Application>
Due to popular demand, here is the “same” example in a more ActionScript friendly format:
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/09/14/toggling-word-wrap-on-a-datagrid-column-header-in-flex/ -->
<mx:Application name="DataGridColumn_headerWordWrap_test"
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="top"
backgroundColor="white"
initialize="init();">
<mx:Script>
<![CDATA[
import mx.containers.ApplicationControlBar;
import mx.containers.Form;
import mx.containers.FormItem;
import mx.controls.CheckBox;
import mx.controls.DataGrid;
import mx.controls.HSlider;
import mx.controls.dataGridClasses.DataGridColumn;
import mx.core.ScrollPolicy;
import mx.events.SliderEvent;
private var checkBox:CheckBox;
private var slider:HSlider;
private var dataGrid:DataGrid;
private var dataGridColumn1:DataGridColumn;
private var dataGridColumn2:DataGridColumn;
private function init():void {
checkBox = new CheckBox();
checkBox.addEventListener(Event.CHANGE,
checkBox_change);
slider = new HSlider();
slider.minimum = 2;
slider.maximum = 10;
slider.value = 6;
slider.snapInterval = 1;
slider.tickInterval = 1;
slider.liveDragging = true;
slider.setStyle("showTrackHighlight", true);
slider.addEventListener(SliderEvent.CHANGE,
slider_change);
var formItem1:FormItem = new FormItem();
formItem1.label = "headerWordWrap:";
formItem1.addChild(checkBox);
var formItem2:FormItem = new FormItem();
formItem2.label = "rowCount:";
formItem2.addChild(slider);
var form:Form = new Form();
form.styleName = "plain";
form.addChild(formItem1);
form.addChild(formItem2);
var appControlBar:ApplicationControlBar;
appControlBar = new ApplicationControlBar();
appControlBar.dock = true;
appControlBar.addChild(form);
addChildAt(appControlBar, 0);
dataGridColumn1 = new DataGridColumn();
dataGridColumn1.dataField = "@name";
dataGridColumn1.headerText = "This is a column with a long title:";
dataGridColumn1.minWidth = 80;
dataGridColumn2 = new DataGridColumn();
dataGridColumn2.dataField = "@price";
dataGridColumn2.headerText = "Price:";
dataGridColumn2.headerWordWrap = false;
dataGridColumn2.minWidth = 20;
dataGrid = new DataGrid();
dataGrid.columns = [dataGridColumn1, dataGridColumn2];
dataGrid.dataProvider = dp.product;
dataGrid.rowCount = 6;
dataGrid.verticalScrollPolicy = ScrollPolicy.ON;
dataGrid.width = 300;
addChild(dataGrid);
}
private function checkBox_change(evt:Event):void {
dataGridColumn1.headerWordWrap = checkBox.selected;
}
private function slider_change(evt:SliderEvent):void {
dataGrid.rowCount = evt.value;
}
]]>
</mx:Script>
<mx:XML id="dp" source="data/products.xml" />
</mx:Application>




Just what I needed. Thanks.
Now, how do I center the header text?
Jeff,
Try this:
<mx:Style> DataGrid { headerStyleName: centerAlignedBold; } .centerAlignedBold { fontWeight: bold; textAlign: center; } </mx:Style>Peter
And just because I love… “Aligning the header text in a DataGrid column in Flex”
Peter
HI,
how to insert button within the columns of data grid.
thanks a lot..
Akhil