Setting the header colors on the DataGrid control in Flex
The following example shows how you can set the header colors on a Flex DataGrid control by setting the headerColors style to an array of two colors to use for the background gradient.
Full code after the jump.
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/01/22/setting-the-header-colors-on-the-datagrid-control-in-flex/ -->
<mx:Application name="DataGrid_headerColors_test"
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="middle"
backgroundColor="white">
<mx:Array id="arr">
<mx:Object c1="1. One" c2="1. Two" c3="1. Three" />
<mx:Object c1="2. One" c2="2. Two" c3="2. Three" />
<mx:Object c1="3. One" c2="3. Two" c3="3. Three" />
<mx:Object c1="4. One" c2="4. Two" c3="4. Three" />
<mx:Object c1="5. One" c2="5. Two" c3="5. Three" />
<mx:Object c1="6. One" c2="6. Two" c3="6. Three" />
<mx:Object c1="7. One" c2="7. Two" c3="7. Three" />
<mx:Object c1="8. One" c2="8. Two" c3="8. Three" />
<mx:Object c1="9. One" c2="9. Two" c3="9. Three" />
</mx:Array>
<mx:DataGrid id="dataGrid"
dataProvider="{arr}"
headerColors="[red, blue]"
verticalScrollPolicy="on">
<mx:columns>
<mx:DataGridColumn dataField="c1" headerText="Column 1" />
<mx:DataGridColumn dataField="c2" headerText="Column 2" />
<mx:DataGridColumn dataField="c3" headerText="Column 3" />
</mx:columns>
</mx:DataGrid>
</mx:Application>
You can also set the headerColors 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/22/setting-the-header-colors-on-the-datagrid-control-in-flex/ -->
<mx:Application name="DataGrid_headerColors_test"
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="middle"
backgroundColor="white">
<mx:Style>
DataGrid {
headerColors: red, blue;
}
</mx:Style>
<mx:Array id="arr">
<mx:Object c1="1. One" c2="1. Two" c3="1. Three" />
<mx:Object c1="2. One" c2="2. Two" c3="2. Three" />
<mx:Object c1="3. One" c2="3. Two" c3="3. Three" />
<mx:Object c1="4. One" c2="4. Two" c3="4. Three" />
<mx:Object c1="5. One" c2="5. Two" c3="5. Three" />
<mx:Object c1="6. One" c2="6. Two" c3="6. Three" />
<mx:Object c1="7. One" c2="7. Two" c3="7. Three" />
<mx:Object c1="8. One" c2="8. Two" c3="8. Three" />
<mx:Object c1="9. One" c2="9. Two" c3="9. Three" />
</mx:Array>
<mx:DataGrid id="dataGrid"
dataProvider="{arr}"
verticalScrollPolicy="on">
<mx:columns>
<mx:DataGridColumn dataField="c1" headerText="Column 1" />
<mx:DataGridColumn dataField="c2" headerText="Column 2" />
<mx:DataGridColumn dataField="c3" headerText="Column 3" />
</mx:columns>
</mx:DataGrid>
</mx:Application>
Or, you can set the headerColors style using ActionScript, as seen in the following example:
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/01/22/setting-the-header-colors-on-the-datagrid-control-in-flex/ -->
<mx:Application name="DataGrid_headerColors_test"
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="middle"
backgroundColor="white">
<mx:Script>
<![CDATA[
private function btn_click(evt:Event):void {
dataGrid.setStyle("headerColors", ["red", "blue"]);
}
]]>
</mx:Script>
<mx:Array id="arr">
<mx:Object c1="1. One" c2="1. Two" c3="1. Three" />
<mx:Object c1="2. One" c2="2. Two" c3="2. Three" />
<mx:Object c1="3. One" c2="3. Two" c3="3. Three" />
<mx:Object c1="4. One" c2="4. Two" c3="4. Three" />
<mx:Object c1="5. One" c2="5. Two" c3="5. Three" />
<mx:Object c1="6. One" c2="6. Two" c3="6. Three" />
<mx:Object c1="7. One" c2="7. Two" c3="7. Three" />
<mx:Object c1="8. One" c2="8. Two" c3="8. Three" />
<mx:Object c1="9. One" c2="9. Two" c3="9. Three" />
</mx:Array>
<mx:Button label="Set headerColors"
click="btn_click(event);" />
<mx:DataGrid id="dataGrid"
dataProvider="{arr}"
verticalScrollPolicy="on">
<mx:columns>
<mx:DataGridColumn dataField="c1" headerText="Column 1" />
<mx:DataGridColumn dataField="c2" headerText="Column 2" />
<mx:DataGridColumn dataField="c3" headerText="Column 3" />
</mx:columns>
</mx:DataGrid>
</mx:Application>
Also, if you wanted to use hex values instead of named values, you could use any of the following syntaxes.
MXML (inline):
<mx:DataGrid headerColors="[#FF0000, 0x0000FF]">
MXML (tags):
<mx:DataGrid id="dataGrid"gt;
<mx:headerColors>
<mx:String>#FF0000</mx:String>
<mx:String>0x0000FF</mx:String>
</mx:headerColors>
</mx:DataGrid>
CSS:
<mx:Style>
DataGrid {
headerColors: #FF0000, '0x0000FF';
}
</mx:Style>
ActionScript:
<mx:Script>
<![CDATA[
private function btn_click(evt:Event):void {
dataGrid.setStyle("headerColors", ["#FF0000", 0x0000FF]);
}
]]>
</mx:Script>
If you wanted a solid/flat fill instead of a linear gradient fill, simply set both colors to the same value, as seen in the following example:
<mx:Style>
DataGrid {
headerColors: haloBlue, haloBlue;
}
</mx:Style>
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.
8 Responses to Setting the header colors on the DataGrid control in Flex
-
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


Is there any way to set each header with a different color?
hi .. i follow ur blog everyday ..its like part of routine now ..so a big thanks for writing up all nice codes … can u plz write an code showing how to dynamically change the color of any row in a datagrid ???? ( i have already asked u this in other post of urs but that one is quite old so i thought i wud ask this in a newer one so that the chances of u reading it are increased :) … i think u do read all the comments but i thought i better not take chances ) …
thanks in advance and keep up the good work :)
yogesh gaur,
Yes, I try and read every comment, but don’t always have time/knowledge to reply to each question.
I haven’t played much with custom renderers and changing background colors in a DataGrid or DataGridColumn, but my hella-smart co-worker Alex Harui’s blog is a really great starting point. For example, check out his “Thinking About Item Renderers” blog post, and specifically check out the BackgroundColor in Item Renderers and BackgroundColor Changes When Data Changes sections.
Happy Flexing,
Peter
Hi Peter ,
thanks for the reply…:)
* 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.
* (also asked in: http://blog.flexexamples.com/2009/01/23/setting-the-column-header-text-color-on-a-datagrid-control-in-flex/ )
Hi Peter,
Is there anyways to set a particular header with a gradient color, my requirement is to add the gradient color to a datagrid column which is sorted….any suggestions
any suggestions ..guys i am stuck!! :(
this is for to color the background of column in datagrid
var col:AdvancedDataGridColumn = new AdvancedDataGridColumn();
col.setStyle(‘backgroundColor’,0xc6d8ff);