6 Replies Latest reply on Aug 17, 2009 7:34 AM by vindictive27

    Resetting selectedIndex in datagrid.

    vindictive27

      Here is my code:

       

       

       

       

       

       

      private function mpOK():void

      {

       

       

           if ((infoCollection.length > 0) && (dgMissionInfo.selectedIndex >= 0))

           {

                infoCollection.removeItemAt(dgMissionInfo.selectedIndex);

                infoCollection.refresh();

                dgMissionInfo.selectedIndex = 0;

           }

       

       

      else

           {

                cmbMissionList.setFocus();

           }

      }

       

      Why doesn't this work the way I'd expect it to?

       

      If there are items in the ArrayCollection, and an item is selected in the datagrid, remove the selected item from the ArrayCollection, refresh the ArrayCollection, and then move the selected index for the datagrid back to the top (first) item in the list.

       

      It seems to me that it just works randomly.  What am I doing wrong?

        • 1. Re: Resetting selectedIndex in datagrid.
          kloysen

          What is going wrong?  My question is if the selected index is always reset but the datagrid item not being removed.  If so I assume its a binding error like not having [Bindable] above the infoCollection declaration.

          • 2. Re: Resetting selectedIndex in datagrid.
            vindictive27 Level 1

            What seems to be happening, is that the data for the selected index appears to be correct, and the item is removed from the datagrid but the 'highlighted' index does not update on the screen.  The wrong row ends up being highlighted on the datagrid.  Is there an update or refresh method for the datagrid that I need to be calling?

            • 3. Re: Resetting selectedIndex in datagrid.
              kloysen Level 1

              This might be a timing error with something being selected that is removed and the component gets confused.  Try adding one of the invalidate methods before resetting the selectedindex to 0.  so it would look like:

               

              dataGrid.invalidateDisplayList(); //this could be any of the invalidate methods im not sure

              dataGrid.selectedIndex = 0;

               

              To me though this seems like the answer should be a lot simpler maybe try putting the selectedIndex into a temp variable and then setting it to 0 before removing the object using the index stored in the temp variable.

               

              Right now im just guessing though because I dont really have time to test it myself.

              1 person found this helpful
              • 4. Re: Resetting selectedIndex in datagrid.
                vindictive27 Level 1

                I still haven't gotten this to work at all, and I've tried a number of different methods since my original post.  Is there a way for me to tell the DataGrid control to just highlight the 'top' item in the list without referencing any indexes?  I want to select something, remove it, and then have it automatically go back and highlight the next thing in the list...

                • 5. Re: Resetting selectedIndex in datagrid.
                  vindictive27 Level 1

                  I was never able to get this to work the way I originally wanted it to, however - there is a way to have it jump ahead to the next item in the index.  I ended up just leaving everything in the ArrayCollection - but I added another flag in there that gets updated depending on what happens to the data, and then I set the selectedIndex to skip around based upon that flag.  Sort of a round about way - but it worked.  Thank you for your help!

                  • 6. Re: Resetting selectedIndex in datagrid.
                    vindictive27 Level 1

                    Unable to get original method working, found a different method that works.