Setting the background alpha and background color of a DataGrid control in Flex

The following example shows how you can set the background alpha and background color of a Flex DataGrid control by setting the backgroundAlpha and backgroundColor styles.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/03/06/setting-the-background-alpha-and-background-color-of-a-datagrid-control-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            private var defaultAlternatingItemColors:Array = [0xF7F7F7, 0xFFFFFF];

            private function checkBox_change(evt:Event):void {
                if (checkBox.selected) {
                    dataGrid.setStyle("alternatingItemColors", defaultAlternatingItemColors);
                } else {
                    dataGrid.setStyle("alternatingItemColors", []);
                }
            }
        ]]>
    </mx:Script>

    <mx:ArrayCollection id="arrColl">
        <mx:source>
            <mx:Array>
                <mx:Object c1="ColumnA.1" c2="ColumnB.1" />
                <mx:Object c1="ColumnA.2" c2="ColumnB.2" />
                <mx:Object c1="ColumnA.3" c2="ColumnB.3" />
                <mx:Object c1="ColumnA.4" c2="ColumnB.4" />
                <mx:Object c1="ColumnA.5" c2="ColumnB.5" />
                <mx:Object c1="ColumnA.6" c2="ColumnB.6" />
                <mx:Object c1="ColumnA.7" c2="ColumnB.7" />
                <mx:Object c1="ColumnA.8" c2="ColumnB.8" />
                <mx:Object c1="ColumnA.9" c2="ColumnB.9" />
            </mx:Array>
        </mx:source>
    </mx:ArrayCollection>

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="backgroundAlpha:">
                <mx:HSlider id="slider"
                        minimum="0.0"
                        maximum="1.0"
                        value="1.0"
                        liveDragging="true" />
            </mx:FormItem>
            <mx:FormItem label="backgroundColor:">
                <mx:ColorPicker id="colorPicker"
                        selectedColor="purple" />
            </mx:FormItem>
            <mx:FormItem label="use alternatingItemColors:">
                <mx:CheckBox id="checkBox"
                        label="[0xF7F7F7, 0xFFFFFF]"
                        selected="true"
                        change="checkBox_change(event);" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <mx:DataGrid id="dataGrid"
            dataProvider="{arrColl}"
            backgroundColor="{colorPicker.selectedColor}"
            backgroundAlpha="{slider.value}"
            width="300"
            rowCount="6"
            verticalScrollPolicy="on" />

</mx:Application>

View source is enabled in the following example.

You can also set the backgroundAlpha, backgroundColor, and alternatingItemColors styles in an external .CSS file or <mx:Style /> block, as shown in the following snippet:

<mx:Style>
    DataGrid {
        backgroundAlpha: 0.25;
        backgroundColor: #FF0000;
        alternatingItemColors: ClassReference(null);
    }
</mx:Style>

Or, you can set the backgroundAlpha, backgroundColor, and alternatingItemColors styles using ActionScript, as seen in the following snippet:

<mx:Script>
    <![CDATA[
        private function init():void {
            dataGrid.setStyle("backgroundAlpha", 0.25);
            dataGrid.setStyle("backgroundColor", 0xFF0000);
            dataGrid.setStyle("alternatingItemColors", null);
        }
    ]]>
</mx:Script>

3 thoughts on “Setting the background alpha and background color of a DataGrid control in Flex

  1. Is there a way to change the color of individual header cells? I’ve found a way to change the color of all the header cells, but not individual ones…

Comments are closed.