1 Reply Latest reply on Dec 7, 2010 11:08 PM by Flex harUI

    SelectedIndex in datagrid

    Ted Moens

      I seem to have the following problem:


      In a datagrid with elements in their normal order I can delete elemets at will no problem.


      myDataProvider.removeItemAt(myDataGrid.selectedIndex) works fine.


      Now if the datagrid is sorted using the column headers, again things work well up to a point.  The point being the removal of the last element from the grid in which case the myDataGrid.selectedIndex *sometimes* (see below) is 1 instead of the expected 0 (there is only one item left in the data provider and of course it's index is 0).  So of course the above line of code causes an index out of bounds exception.


      Under what conditions does the problem hit? The order of deletion of the last two elements matters!!!  If you first delete the item that would"normally" come first (but is second in the resorted dataGrid) then there is no problem deleting the other element.  If you first delete the item that would normally come second (but is first in the resorted list) this is when the fumits hit the windmill.


      Note however that in precisely the same scenatio if instead of using myDataGrid.selectedIndex, I grab myDataGrid.selectedItem and iterate through myDataProvider looking for the selected item and then removing it, things work just fine.


      Does this sound familiar to anyone?