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>
 
Tagged with:
 
About The Author

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.

7 Responses to Changing the default sort arrow skin on a Flex DataGrid control

  1. Steve says:

    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.

  2. Yaniv says:

    Exactly my problem, Steve :)
    I need it as well, Help us oh mighty guru ! :)

  3. peterd says:

    Steve/Yaniv,

    For an example, see “Displaying the sort arrow in a Flex DataGrid control without having to click a column”.

    Peter

    PS: A big thanks to my coworker Rob for helping me with the solution. And by “help”, I mean I stole his solution. :)

  4. A Visitor says:

    How are you setting the thumb up icon?

  5. Kevin says:

    How is the thumb-up icon being displayed?

    • Peter deHaan says:

      @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

  6. Ian says:

    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.