Setting the column header text color on a DataGrid control in Flex

by Peter deHaan on January 23, 2009

in Color, DataGrid

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.

View MXML

<?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:

View MXML

<?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:

View MXML

<?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:

View MXML

<?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>

{ 5 comments… read them below or add one }

1 Raj February 24, 2009 at 11:41 am

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.

Reply

2 senthil July 2, 2009 at 1:34 pm

Can we freeze first columns in datagrid and set scrollable for remaining columns

- Senthil

Reply

3 Peter deHaan July 2, 2009 at 8:52 pm
4 Brian Sweat December 2, 2009 at 4:44 pm

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?

Reply

5 Anonymous December 7, 2009 at 9:23 pm

Brian, you will need to set headerRenderer property of the column to a class which you can design as needed.

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree

Previous post:

Next post: