Setting the column header text color on a DataGrid control in Flex
The following example shows how you can set the text color of a Flex DataGrid control’s column header text by setting the headerStyleName and color styles.
Full code after the jump.
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/01/23/setting-the-column-header-text-color-on-a-datagrid-control-in-flex/ -->
<mx:Application name="DataGrid_headerStyleName_color_test"
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="middle"
backgroundColor="white">
<mx:Style>
.myHeaderStyles {
color: red;
fontWeight: bold;
}
</mx:Style>
<mx:Array id="arrDP">
<mx:Object c="1" c1="One" c2="Two" c3="Three" />
<mx:Object c="2" c1="One" c2="Two" c3="Three" />
<mx:Object c="3" c1="One" c2="Two" c3="Three" />
<mx:Object c="4" c1="One" c2="Two" c3="Three" />
<mx:Object c="5" c1="One" c2="Two" c3="Three" />
<mx:Object c="6" c1="One" c2="Two" c3="Three" />
<mx:Object c="7" c1="One" c2="Two" c3="Three" />
<mx:Object c="8" c1="One" c2="Two" c3="Three" />
<mx:Object c="9" c1="One" c2="Two" c3="Three" />
<mx:Object c="10" c1="One" c2="Two" c3="Three" />
<mx:Object c="11" c1="One" c2="Two" c3="Three" />
</mx:Array>
<mx:DataGrid id="dataGrid"
dataProvider="{arrDP}"
headerStyleName="myHeaderStyles" />
</mx:Application>
You can also set the headerStyleName style in an external .CSS file or <Style> block, as seen in the following example:
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/01/23/setting-the-column-header-text-color-on-a-datagrid-control-in-flex/ -->
<mx:Application name="DataGrid_headerStyleName_color_test"
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="middle"
backgroundColor="white">
<mx:Style>
DataGrid {
headerStyleName: myHeaderStyles;
}
.myHeaderStyles {
color: red;
fontWeight: bold;
}
</mx:Style>
<mx:Array id="arrDP">
<mx:Object c="1" c1="One" c2="Two" c3="Three" />
<mx:Object c="2" c1="One" c2="Two" c3="Three" />
<mx:Object c="3" c1="One" c2="Two" c3="Three" />
<mx:Object c="4" c1="One" c2="Two" c3="Three" />
<mx:Object c="5" c1="One" c2="Two" c3="Three" />
<mx:Object c="6" c1="One" c2="Two" c3="Three" />
<mx:Object c="7" c1="One" c2="Two" c3="Three" />
<mx:Object c="8" c1="One" c2="Two" c3="Three" />
<mx:Object c="9" c1="One" c2="Two" c3="Three" />
<mx:Object c="10" c1="One" c2="Two" c3="Three" />
<mx:Object c="11" c1="One" c2="Two" c3="Three" />
</mx:Array>
<mx:DataGrid id="dataGrid"
dataProvider="{arrDP}" />
</mx:Application>
Or, you can set the headerStyleName style using ActionScript, as seen in the following example:
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/01/23/setting-the-column-header-text-color-on-a-datagrid-control-in-flex/ -->
<mx:Application name="DataGrid_headerStyleName_color_test"
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="middle"
backgroundColor="white">
<mx:Style>
.myHeaderStyles {
color: red;
fontWeight: bold;
}
</mx:Style>
<mx:Script>
<![CDATA[
private function btn_click(evt:MouseEvent):void {
dataGrid.setStyle("headerStyleName", "myHeaderStyles");
}
]]>
</mx:Script>
<mx:Array id="arrDP">
<mx:Object c="1" c1="One" c2="Two" c3="Three" />
<mx:Object c="2" c1="One" c2="Two" c3="Three" />
<mx:Object c="3" c1="One" c2="Two" c3="Three" />
<mx:Object c="4" c1="One" c2="Two" c3="Three" />
<mx:Object c="5" c1="One" c2="Two" c3="Three" />
<mx:Object c="6" c1="One" c2="Two" c3="Three" />
<mx:Object c="7" c1="One" c2="Two" c3="Three" />
<mx:Object c="8" c1="One" c2="Two" c3="Three" />
<mx:Object c="9" c1="One" c2="Two" c3="Three" />
<mx:Object c="10" c1="One" c2="Two" c3="Three" />
<mx:Object c="11" c1="One" c2="Two" c3="Three" />
</mx:Array>
<mx:Button id="btn"
label="Set headerStyleName"
click="btn_click(event);" />
<mx:DataGrid id="dataGrid"
dataProvider="{arrDP}" />
</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/2009/01/23/setting-the-column-header-text-color-on-a-datagrid-control-in-flex/ -->
<mx:Application name="DataGrid_headerStyleName_color_test"
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="middle"
backgroundColor="white"
preinitialize="init();">
<mx:Script>
<![CDATA[
import mx.controls.DataGrid;
private var arrDP:Array;
private var dataGrid:DataGrid;
private var dgHeaderStyles:CSSStyleDeclaration;
private function init():void {
arrDP = [];
arrDP.push({c:1, c1:"One", c2:"Two", c3:"Three"});
arrDP.push({c:2, c1:"One", c2:"Two", c3:"Three"});
arrDP.push({c:3, c1:"One", c2:"Two", c3:"Three"});
arrDP.push({c:4, c1:"One", c2:"Two", c3:"Three"});
arrDP.push({c:5, c1:"One", c2:"Two", c3:"Three"});
arrDP.push({c:6, c1:"One", c2:"Two", c3:"Three"});
arrDP.push({c:7, c1:"One", c2:"Two", c3:"Three"});
arrDP.push({c:8, c1:"One", c2:"Two", c3:"Three"});
arrDP.push({c:9, c1:"One", c2:"Two", c3:"Three"});
arrDP.push({c:10, c1:"One", c2:"Two", c3:"Three"});
arrDP.push({c:11, c1:"One", c2:"Two", c3:"Three"});
dgHeaderStyles = new CSSStyleDeclaration(".myHeaderStyles");
dgHeaderStyles.setStyle("color", "red");
dgHeaderStyles.setStyle("fontWeight", "bold");
dataGrid = new DataGrid();
dataGrid.dataProvider = arrDP;
dataGrid.setStyle("headerStyleName", "myHeaderStyles");
addChild(dataGrid);
}
]]>
</mx:Script>
</mx:Application>
Peter deHaan
Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.
-
Add Widgets (Content Sidebar)
This is your Content Sidebar. Edit this content that appears here in the widgets panel by adding or removing widgets in the Content Sidebar area.
5 Responses to Setting the column header text color on a DataGrid control in Flex
Leave a Reply Cancel reply
-
Categories
- Accordion
- AccordionHeader
- ActionScript
- AddChild
- AdvancedDataGrid
- Alert
- alpha
- Animate
- AnimateProperties
- Application
- Application (Spark)
- ArrayCollection
- BarChart
- baseColor
- beta
- beta1
- beta2
- Bitmap
- Bitmap/BitmapData
- BitmapData
- BitmapFill
- BitmapFill (Spark)
- BitmapGraphic
- BitmapImage
- BitmapImage (Spark)
- BitmapImageResizeMode
- Border (Spark)
- BorderContainer (Spark)
- Box
- BuildInfo
- Button
- Button (Spark)
- ButtonBar
- ButtonBar (Spark)
- ByteArray
- Camera
- Charting
- CheckBox
- CheckBox (Spark)
- ClassFactory
- CollectionEvent
- Color
- ColorPicker
- ColorUtil
- ComboBox
- ComboBoxArrowSkin
- Compiler
- Component
- Component (Spark)
- Configuration
- Container
- ContextMenu
- ContextMenuEvent
- ContextMenuItem
- CSSCondition
- CSSSelector
- CSSStyleDeclaration
- CurrencyFormatter
- CursorManager
- Data Binding
- DataGrid
- DataGrid (Spark)
- DataGridColumn
- Date
- DateBase
- DateChooser
- DateField
- DateFormatter
- Debugging
- DefaultComplexItemRenderer
- DefaultTileListEffect
- DropDownList
- DropDownList (Spark)
- DropDownListButtonSkin
- DropDownListSkin
- DropShadowFilter
- E4X
- Effects
- Ellipse
- EmailValidator
- Embed
- Event
- Fade
- FileFilter
- FileReference
- fill
- Filters
- Flash
- Flash Integration
- FlashVars
- Flex 3 SDK
- Flex Builder
- Flex Builder 3
- Flex SDK
- Flex4
- FLVPlayback
- FocusManager
- FontLookup
- Fonts
- Form
- Form (Spark)
- FormHeading (Spark)
- FormItem
- FormItem (Spark)
- Forms
- FTETextField (Spark)
- FullScreen
- FullScreenEvent
- FxAnimateColor
- FxButtonBar
- FxCheckBox
- FXG
- FxHScrollBar
- FxHSlider
- FxList
- FxNumericStepper
- FxRadioButton
- FxRotate3D
- FxScroller
- FxTextArea
- FxTextInput
- FxToggleButton
- FxVScrollBar
- FxVSlider
- getStyleDeclaration()
- GradientEntry
- Graphic (Spark)
- HBox
- HDividedBox
- HGroup (Spark)
- HorizontalLayout
- HorizontalList
- HSBColor (Spark)
- HScrollBar (Spark)
- HSlider
- HSlider (Spark)
- HTML template
- ID3Info
- Image
- Image (Spark)
- ImageSnapshot
- itemRenderer
- JointStyle
- Label
- Label (Spark)
- Legend
- LegendItem
- LigatureLevel
- Line
- LinearGradientStroke
- LineScaleMode
- LinkBar
- LinkButton
- List
- List (Spark)
- Menu
- MenuBar
- Metadata
- MetadataEvent
- Model
- Mouse
- MouseCursor
- MouseEvent
- Move
- Namespace
- NavigatorContent (Spark)
- needsSWF
- NetConnection
- NetStream
- Nightly Builds
- NumberBaseRoundType
- NumberFormatter
- NumberValidator
- NumericCompare
- NumericStepper
- NumericStepper (Spark)
- ObjectProxy
- ObjectUtil
- paddingLeft
- paddingRight
- Panel
- Panel (Spark)
- Parallel
- Path
- PieChart
- PieSeries
- PieSeriesItem
- PopUpAnchor (Spark)
- PopUpButton
- PopUpManager
- ProgrammaticSkin
- ProgressBar
- PropertyChangeEvent
- QName
- RadialGradient
- RadioButton
- RadioButton (Spark)
- RadioButtonGroup
- RadioButtonGroup (Spark)
- Rect
- RegExp
- Regular Expressions
- Repeater
- RichEditableText
- RichText
- RichText (Spark)
- RichTextEditor
- Rotate
- Rotate3D (Spark)
- Scroller (Spark)
- Sequence
- setStyle()
- SimpleText
- SimpleText (Spark)
- skinClass
- Slider
- SliderEvent
- SolidColor
- SolidColorStroke
- Sort
- SortField
- Sound
- SoundEffect
- Spinner (Spark)
- SpriteVisualElement (Spark)
- StageDisplayState
- States
- StringUtil
- StringValidator
- StyleManager
- Styles
- SWFLoader
- SWFObject
- System
- SystemManager
- TabBar
- TabBar (Spark)
- TabNavigator
- TabStopFormat
- Text
- Text Layout Framework (TLF)
- TextArea
- TextArea (Spark)
- TextBox
- TextConverter
- TextEvent
- TextFlow
- TextFlowUtil
- TextFormat
- TextGraphic
- TextInput
- TextInput (Spark)
- TextLayoutFormat
- TextView
- Themes
- TileLayout
- TileList
- TileOrientation
- Timer
- TitleWindow
- TitleWindow (Spark)
- TLF
- ToggleButton (Spark)
- ToggleButtonBar
- ToolTip
- Transition
- Tree
- TruncationOptions
- UIComponent
- UIFTETextField
- Updater
- URLLoader
- URLRequest
- URLUtil
- URLVariables
- ValidationResultEvent
- Validator
- Validators
- VBox
- VDividedBox
- Vector
- VerticalLayout
- VerticalLayout (Spark)
- VGroup (Spark)
- Video
- VideoDisplay
- VideoElement
- VideoElement (Spark)
- VideoEvent
- VideoPlayer (Spark)
- VideoPlayerScrubBar
- ViewStack
- VScrollBar (Spark)
- VSlider
- VSlider (Spark)
- XML
- XMLList
- XMLListCollection
- ZipCodeValidator
- ZipCodeValidatorDomainType
- Zoom
-
Articles
- December 2010
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
-
Meta


Does anyone know how to use styles to set the rollover color of the header text in a datagrid? I have white text on a dark background in the header of my datagrid and can get the background color to change on the rollover but not the header text color.
Can we freeze first columns in datagrid and set scrollable for remaining columns
- Senthil
senthil,
Yes, http://blog.flexexamples.com/2007/08/15/locking-columns-in-a-horizontally-scrolling-datagrid-control/.
Peter
Do you know if it’s possible to use multiple font colors in the headerText for a single column?
For example, something like “Good/Bad” with Good in green and Bad in red. Any suggestions?
Brian, you will need to set headerRenderer property of the column to a class which you can design as needed.