The following code demonstrates a bug when you assign to the
DataGrid.selectedItem property when the underlying dataProvider is an
arrayCollection using a sort assigned to it.  If you comment out the "
griddata.sort = sort", the bug goes away (as does your sort,
unfortunately).  I tracked down through the ListCollectionView source code
and everything seems work fine as far as figuring out which item should be
selected.  But it never seems to apply that to selectedItem and instead
applies it only to selectedItems.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"; layout="vertical"
creationComplete="creationComplete()">
 <mx:Script>
   <![CDATA[
     import mx.collections.SortField;
     import mx.collections.Sort;
     import mx.collections.ArrayCollection;
     [Bindable]
     private var griddata:ArrayCollection = new ArrayCollection([
         {name:"Tim", id:42},
         {name:"Jill", id:21},
         {name:"Phil", id:99},
         {name:"Sally", id:66}
       ]);

     private function creationComplete():void
     {
       var sort:Sort = new Sort;
       sort.fields = [new SortField("name", true), new SortField("id",
false, false, true)];
       // comment out the next line and run again to see it without the
problem
       griddata.sort = sort;
       griddata.refresh();
     }

     private function btnClick():void
     {
       grid.selectedItem = griddata[1];
       txt.text = (grid.selectedItem ? "selectedItem.name = " +
grid.selectedItem.name : "selectedItem = null");
       txt2.text = (grid.selectedItems.length ? "selectedItems[0].name = "
+ grid.selectedItems[0].name : "selectedItem[0] = null");
     }
   ]]>
 </mx:Script>
 <mx:DataGrid id="grid" dataProvider="{griddata}">
   <mx:columns>
     <mx:DataGridColumn dataField="name"/>
     <mx:DataGridColumn dataField="id"/>
   </mx:columns>
 </mx:DataGrid>
 <mx:Button label="click" click="btnClick()"/>
 <mx:Text id="txt"/>
 <mx:Text id="txt2"/>
</mx:Application>

I have run into similar issues before, but this is the first time I've
managed to figure out that the sort was the trigger.  I have entered this on
the Adobe bug page.

--
Jason

Reply via email to