The cool thing when you develop in Flex is that you have many solutions to reach the same goal.
With the selectedItem property of the DataGrid you get the object selected in the row by the user. This object contains all information about the selected index of your ArrayCollection.
You can cast this object to a custom Actionscript class that works as Data Model or Value Object for your application.
See this example.
This is my Actionscript data model (User.as):
package com.casario.vo
{
[Bindable] public class User
{
// You should avoid the use of public property
// and use the getter/setter mothods
public var firstName:String;
public var lastName:String;
public var email:String;
}
}
And this is my DataGrid with the addEventListener method that register its change event with an event handler :
<mx:DataGrid
width="100%"
id="myDG"
>
myDG.addEventListener( MouseEvent.CLICK, clickHandler );
The event hanlder just uses the as keyword to cast the object into our Actionscript data model
private function clickHandler( event:MouseEvent ):void
{
var myASDM:User = DataGrid( event.currentTarget ).selectedItem as User;
}
To access the selected item in the event handler I used the currentTarget property of the event object data typed as MouseEvent.
Now we are able to use the myASDM and access to its properties using the dot syntax :
myASDM.firstName; myASDM.lastName; myASDM.email






















Well, why not adding a listener on ItemClick?
I feel it is quite simpler than this.
I also think that your datagrid will badly react to a mouse click on a column header...
Posted by: Jiben | April 19, 2007 at 05:31 PM
//Well, im using this over AS3 for Flash
//
myDatagrid.addEventListener(Event.CHANGE, selectItemEvent);
function selectItemEvent(e:Event) {
trace("# selectItemEvent");
select_btn.enabled=true;
drawNow()
}
// as you can see, after user selects, a button comes available (select_btn.enabled=true) to the user.
// Try exploring all the events possible, overall the events that are inherited from other classes.
Posted by: AA | July 27, 2008 at 05:14 AM
Hi, I've tried your method but it doesn't work for me.
The variable where I try to store the selectedItem object is null!!
I'm using the Flex + Cairngorm framework + WebOrb.
this my VO:
package com.adobe.cairngorm.samples.login.vo
{
import com.adobe.cairngorm.vo.IValueObject;
[RemoteClass(alias="com.adobe.cairngorm.samples.login.vo.MenuTableVO")]
[Bindable]
public class MenuTableVO implements IValueObject
{
public var language_id : int;
public var language_value : String;
}
}
and this is my mxml file:
import com.adobe.cairngorm.samples.login.vo.MenuTableVO;
...
[Bindable]
private var selectedRecord : MenuTableVO;
...
private function fillSelectedRecordset(e : MouseEvent):void
{
selectedRecord = MenuGrid(e.currentTarget).selectedItem as MenuTableVO;
}
...
Posted by: Emanuele | September 16, 2008 at 11:54 AM