Programmatically getting DataGrid to sort

I’ve just had a merry time getting a DataGrid to sort by a particular column (and show the apt arrow in the header) when it first renders i.e. as if the user had clicked the header.

Googling on the subject suggested (http://blog.flexexamples.com/2008/02/28/displaying-the-sort-arrow-in-a-flex-datagrid-control-without-having-to-click-a-column/) that defining dataProvider.sort then calling dataProvider.refresh() would have the desired effect – however for me it didn’t work: the arrow got displayed, but did not sort the column properly – until the user actually clicked the header, at which point it all came good (and continued to re-sort when items were added).

In the end I had to use a rather literal solution to the problem i.e. simulate the user clicking the header by dispatching a DataGridEvent.HEADER_RELEASE event:

    ...
    var eventOut:DataGridEvent = new DataGridEvent(DataGridEvent.HEADER_RELEASE, false, false, myColIndex);
    myGrid.dispatchEvent(eventOut);
    ...

Not ideal, but it worked – so posting here in case it helps anyone else.

kdw.

Leave a Reply

Search
Categories