2 Replies Latest reply on Oct 31, 2007 7:26 AM by mark416

    How to get DataGrid selected item???

    mark416 Level 1
      Hi,

      I have a datagrid

      <mx:DataGrid x="10" y="36" dataProvider="{acEmaillist}" width="319">
      <mx:columns>
      <mx:DataGridColumn headerText="Full Name" dataField="cn" width="150"/>
      <mx:DataGridColumn headerText="Email" dataField="mail" width="150"/>
      </mx:columns>
      </mx:DataGrid>

      I want to select a Name , click it , get the selected Name and save it to a string variable.Please let me know how to get the selected name when I click it.Thanks a lot.

      M
        • 1. Re: How to get DataGrid selected item???
          Gregory Lafrance Level 6
          This code may help.

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" width="100%">
          <mx:Script>
          <![CDATA[
          import mx.controls.DataGrid;
          import mx.events.ListEvent;

          [Bindable]
          private var nameStr:String;

          public function getName(event:ListEvent):void {
          var dg:DataGrid = DataGrid(event.target);
          nameStr = dg.selectedItem.cn;
          }
          ]]>
          </mx:Script>
          <mx:ArrayCollection id="acEmaillist">
          <mx:Object>
          <mx:cn>Bob Smith</mx:cn>
          <mx:mail>bsmith@bsmith.com</mx:mail>
          </mx:Object>
          <mx:Object>
          <mx:cn>Ted Alan</mx:cn>
          <mx:mail>talan@bsmith.com</mx:mail>
          </mx:Object>
          <mx:Object>
          <mx:cn>Fred Tobs</mx:cn>
          <mx:mail>ftobs@bsmith.com</mx:mail>
          </mx:Object>
          </mx:ArrayCollection>

          <mx:DataGrid dataProvider="{acEmaillist}" itemClick="getName(event)">
          <mx:columns>
          <mx:DataGridColumn headerText="Full Name" dataField="cn" width="150"/>
          <mx:DataGridColumn headerText="Email" dataField="mail" width="150"/>
          </mx:columns>
          </mx:DataGrid>
          <mx:Label text="{nameStr}"/>
          </mx:Application>
          • 2. Re: How to get DataGrid selected item???
            mark416 Level 1
            good job! Greg

            The code works fine.Thanks.

            M