1 Reply Latest reply on Nov 25, 2007 10:48 AM by ntsiii

    datagrid selectedIndex bug?

    N00bie_XL
      See my code and I'll explain my question below:

      import mx.controls.Alert;
      import mx.collections.*;
      import mx.rpc.events.ResultEvent;
      import com.adobe.serialization.json.JSON;
      import flash.events.KeyboardEvent;

      [Bindable]
      public var mails:ArrayCollection;

      public function onJSONLoad(event:ResultEvent):void
      {
      mails = new ArrayCollection(JSON.decode(String(event.result)));
      spamGrid.dataProvider = mails;
      }

      public function myKeyUpHandler(event:KeyboardEvent):void
      {
      var KEY_DELETE:uint = 46;
      var sIndex:int = spamGrid.selectedIndex;
      if ((event.keyCode == Keyboard.DELETE) && (sIndex > -1))
      {
      mails.removeItemAt(sIndex);
      spamGrid.dataProvider = mails;
      spamGrid.selectedIndex = 0;
      }
      }

      Now when I select a row in de datagrid wich index is greater than 0 and I hit the delete key. The selected row will be deleted and the selectedIndex of the datagrid is set to 0. Everything goes fine here, but when I delete the row with index 0, this row will also be deleted but the selectedIndex after the delete will be set to 1. But in my code I do set it to 0.

      Any suggestions for the reason if I delete a row at index 0 the new selectedIndex is 1 instead of 0?

      Thx !

      Clifford James
        • 1. Re: datagrid selectedIndex bug?
          ntsiii Level 3
          After assigning or manipulating a dataProvider, you often need to let the visual control finish updating before you attempt any other operations on it.

          Try this:
          ...
          mails.removeItemAt(sIndex);
          spamGrid.dataProvider = mails;
          callLater(selectFirstRow);
          }

          private function selectFirstRow():void {
          spamGrid.selectedIndex = 0;
          }

          Tracy