Moris' Note Book

本博客所有内容皆收集于网上,非本人原创,非心情日记,非研究心得,只是自己浏览的东西的收集
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

A issue related to "incorect SelectedIndex when remove a row from DataGrid"

Posted on 2009-03-20 12:06  moris  阅读(165)  评论(0)    收藏  举报

Ref to: http://www.airia.cn/FLEX_Directory/accessing_xml_data/2/

This is a KNOWN issue, when you remove a row from a datagrid and set next row as "selected" with SelectedIndex, sometime a "并非所愿" item is selected, the following is the solution:

1. remove the item from data source

2. define a function to flush the data source to grid, and reset the selectedIndex

2. use callLater to call the function.

the following is a sample:

          private function deleteBookHandler():void
         {
                // Save the currently selected index.
                var selectedBookIndex:int = myDataGrid.selectedIndex;
                                               
                // Delete the currently selected book.

                delete (myBooks.book[selectedBookIndex]);
               
                // Reselect the next logical item in the data grid.
                newSelectedIndex = (selectedBookIndex==0) ? 0 : selectedBookIndex - 1;

                // Change the selected index of the data grid

                // at a later frame. See note on changeDataGridIndex()
                // method for more details on this workaround.
                callLater ( changeDataGridIndex );           
            }  

            // This is a workaround for a known issue with
            // List-based components where deleting an item
            // from the control's dataProvider leaves the

            // selectedIndex at an incorrect value. The workaround
            // is to reassign the data provider at least a
            // frame later and to change the index there.
            private function changeDataGridIndex ():void
            {

                // Reassign the data grid's data provider.
                myDataGrid.dataProvider = myBooks.book;

                // Set the selected index.
                myDataGrid.selectedIndex = newSelectedIndex;

                 }