3 Replies Latest reply on Aug 4, 2006 5:02 PM by ntsiii

    Datagrid selectedItem Problem Using Variables

    Ticean_Bennett Level 1
      I'm trying to dynamically set the selectedIndex of a datagrid.

      It works fine when I user a hard coded int like: dg.selectedIndex = 4.

      When I try to use a variable, that is an int within bounds, the selection is not changed. Have i found a bug? I've tried everything and I'm pretty frustrated at this point.

      Here's my code:

      quote:


      private function moveRow( direction:int):void
      {
      var sIndex:int = dg.selectedIndex;
      var newIndex:int = dg.selectedIndex + direction;
      var sObject:Object;
      var newObject:Object;

      // Check to make sure the move is within bounds.
      if (!((newIndex < 0) || (newIndex >= dataProvider.length)))
      {
      // Make copies of the two objects.
      sObject = dataProvider[sIndex];
      newObject = dataProvider[newIndex];

      // Replace the objects in their new order.
      dataProvider.setItemAt(sObject, newIndex);
      dataProvider.setItemAt(newObject, sIndex);
      }

      // ReAssign new sequence to all in the list.
      for(var i:int = 0; i < dataProvider.length; i++ )
      {
      // Alert.show(dataProvider .Name + ": " + dataProvider.DisplaySequence);
      dataProvider .DisplaySequence = i+1;
      }

      // Select the item position to which we moved.
      dg.selectedIndex = newIndex;

      // Clean up.
      sObject = null;
      newObject = null;
      }



      Any help would be much appreciated...
        • 1. Re: Datagrid selectedItem Problem Using Variables
          isml Level 1
          Try to use callLater:

          callLater(SetSelect, [newIndex]);

          private function SetSelect(newIndex):void
          {
          dg.selectedIndex = newIndex;
          }

          Maybe it can work
          • 2. Re: Datagrid selectedItem Problem Using Variables
            Ticean_Bennett Level 1
            Thanks, that did work.

            But I don't understand why its an issue. Can anyone explain? Everything in my code was working; up unti I set the selection. Placing a hardcoded int value on the set selection worked. I know that newIndex was valid. I traced out in that code section.

            It would make sense if i had removed items from the dataProvider. In that case the items would be removed from the datagrid - but the screen representation not updated until the next screen redraw (frame). But neither the length of the source ArrayList or Datagrid changed.

            ...or am I just overlooking the inner workings of ArrayCollection.setItemAt()?
            • 3. Re: Datagrid selectedItem Problem Using Variables
              ntsiii Level 3
              I don't know Flash development, but i do know that functions and data operations run in one frame then the UI is updated in the next. So the way you were doing it, the Selected index might have been getting set, but the UI was not updating.

              callLater is the solution.

              Tracy