How to deselect a flex datagrid row by clicking on it

apache-flexdatagridflex3

I have a question for a simple thing that there doesn't seem to be a simple solution to. I have a datagrid, and the rows should be deselected if it's clicked on and already selected. How to do that?

I'm looking at the different "item"-events but the row is already selected when they are dispatched, so there is no way to tell if it was already selected or not.

Can someone help me out here with a very simple thing that I've probably missed?

Best Solution

Try this :

<?xml version="1.0" encoding="utf-8"?>
<mx:Application
    xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

    <mx:Array id="arr1">
        <mx:Object label="One fish" />
        <mx:Object label="Two fish" />
        <mx:Object label="Tree fish" />
        <mx:Object label="Four fish" />
    </mx:Array>

    <mx:DataGrid id="dataGrid2"
                    click="test(event)"
                    dataProvider="{arr1}"
                    draggableColumns="false"
                    width="100%"
                    height="100%" >
                    <mx:columns>
            <mx:DataGridColumn dataField="label" width="50" />
        </mx:columns>
    </mx:DataGrid>

    <mx:Script>
        <![CDATA[

            private var lastIndex :Number = -1;
            private function test(evt:MouseEvent):void
            {
                if(dataGrid2.selectedIndex == lastIndex)
                    dataGrid2.selectedIndex = -1;

                lastIndex = dataGrid2.selectedIndex;
            }

        ]]>
    </mx:Script>

</mx:Application>