Changing the default sort arrow skin on a Flex DataGrid control

The following example shows how you can change the sort arrow skin on a DataGrid control in Flex by setting the sortArrowSkin style.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/02/23/changing-the-default-sort-arrow-skin-on-a-flex-datagrid-control/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Array id="arr">
        <mx:Object c1="Item 1A" c2="Item 1B" />
        <mx:Object c1="Item 2A" c2="Item 2B" />
        <mx:Object c1="Item 3A" c2="Item 3B" />
        <mx:Object c1="Item 4A" c2="Item 4B" />
        <mx:Object c1="Item 5A" c2="Item 5B" />
        <mx:Object c1="Item 6A" c2="Item 6B" />
        <mx:Object c1="Item 7A" c2="Item 7B" />
        <mx:Object c1="Item 8A" c2="Item 8B" />
        <mx:Object c1="Item 9A" c2="Item 9B" />
    </mx:Array>

    <mx:DataGrid id="dataGrid"
            dataProvider="{arr}"
            sortArrowSkin="@Embed('assets/thumb_down.png')"
            width="300"
            rowCount="8">
        <mx:columns>
            <mx:DataGridColumn dataField="c1"
                    headerText="Column 1:" />
            <mx:DataGridColumn dataField="c2"
                    headerText="Column 2:" />
        </mx:columns>
    </mx:DataGrid>

</mx:Application>

View source is enabled in the following example.

You can also set the sortArrowSkin style in an external .CSS file or <mx:Style /> block, as shown in the following snippet:

<mx:Style>
    DataGrid {
        sortArrowSkin: Embed("assets/thumb_down.png");
    }
</mx:Style>

Or, you can set the sortArrowSkin style using ActionScript, using the following snippet:

<mx:Script>
    <![CDATA[
        [Embed("assets/thumb_down.png")]
        private var thumbDownIcon:Class;

        private function init():void {
            dataGrid.setStyle("sortArrowSkin", thumbDownIcon);
        }
    ]]>
</mx:Script>

7 thoughts on “Changing the default sort arrow skin on a Flex DataGrid control

  1. Thanks for another great Flex tip. One question I have about the sort arrow is how do you get the sort arrow to appear without having to click on a column header? I have an app where I need the arrow to show after the data loads.

    1. @Kevin/A Visitor,

      Using the sortArrowSkin style. The sortArrowSkin skin is just flipped vertically depending on whether it is an ascending sort or descending sort.

      <mx:DataGrid id="dataGrid" ... sortArrowSkin="@Embed('assets/thumb_down.png')" />

      Peter

  2. I know this blog is really old, but I have a correlating question. Is there any way to change the position of the sort arrow?? I’d like to have it appear just left/right of the column header text.

Comments are closed.