1 Reply Latest reply on Sep 24, 2010 11:11 AM by jfb00

    Can't pre-select item in DataGrid (Array)

    RandomReado Level 1

      I'm polling a RemoteObject every 5 seconds using setInterval and the returned result (Array) is being fed into a DataGrid as the DataProvider. However everytime this happens the selected row deselects when the DataGrid is refreshed. So I want to re-select the item when the DataGrid has been updated.

       

      So far I've tried capturing the selected row before the RemoteObject is called using:

       

      private function refreshDataGrid(e:ResultEvent):void
      {
          var selectedRow:Object = myDataGrid.selectedItem;
          myDataGrid.dataProvider = e.result as Array;
          myDataGrid.selectedItem = selectedRow;
      }
      

       

      However this doesn't work. If I select the row and then do a "trace(myDataGrid.selectedItem)", the result in the Console is blank.

       

      In another attempt I tried:

       

      private function refreshDataGrid(e:ResultEvent):void
      {
          var selectedItem:* = myDataGrid.selectedItem.itemId;
          myDataGrid.dataProvider = e.result as Array;
          myDataGrid.selectedItem.itemId = selectedItem;
      }
      

       

      This doesn't work either.

       

      Can anyone help me make this work? Any help would be greatly appreciated. Thanks in advance.

        • 1. Re: Can't pre-select item in DataGrid (Array)
          jfb00 Level 3

          Hi,

          Try this aproach.

          Create an arrayCollection like:

          import mx.collections.ArrayCollection;
          private var myArray:ArrayCollection = new ArrayCollection;

           

          set this array as data provider for your datagrid like:


          <mx:DataGrid id="myDatagrid"  dataProvider="{myArray}">


          now when you get the result:
          private function refreshDataGrid(e:ResultEvent):void
          {
          var selectedRow:int = myDatagrid.selectedIndex;
          myArray =  ArrayCollection(event.result);
          myDatagrid.selectedIndex = selectedRow;

          }

          I'm not sure if this work but maybe we are close, if you can create a sample app and post here the code, I can test and fix it for you.

           

          Johnny

          Please rate my answer