Flex - Preventing DataGrid scrolling when the dataprovider is updated.

I was having issues with my DataGrid scrolling to the top when my grid updated from the server. It is very aggravating as a user when you are milling about and *BAM* it scrolls away.

This worked:

var storeItemForUpdate:ItemVO = grid.selectedItem;;
var vScroll:int = grid.verticalScrollPosition;
arrayCollection.source = updatedDataArray
grid.validateNow();
grid.verticalScrollPosition = vScroll;
grid.selectedItem = storeItemForUpdate;

The current selection is stored, the scroll position is stored, the grid is updated, validated, and then the stored variables are applied without so much as a flicker on the screen for the user. Much better.

3 Responses to “Flex - Preventing DataGrid scrolling when the dataprovider is updated.”


  1. 1 Joel

    I found that it is easier to update the dataProvider with itemUpdated and make proper use of collections in Flex. This is still a solution if that isn’t possible, for whatever reason, but I’d say it is far from a best practice.

  2. 2 Hari

    Hi, my datagrid is bound to an ArrayCollection which is the dataprovider. When I remove items from Arraycollection, the focus on the datagrid moves to the bottom of the grid which deiplays a blank row. I want the focus to be remained always at the top. Please let me know any way to solve this.

  3. 3 Joel

    I’ve never seen it go to the bottom like that!

    If you want it to go to the top, do this:

    myDataGrid.selectedIndex = 0
    myDataGrid.scrollToIndex(0)

    That will reposition and select the first entry. To make it ’smart’ you want to record the position of the item you are deleting, and have it select/scroll there.

Leave a Reply