2 Replies Latest reply on Jun 15, 2006 10:00 AM by is2

    Can we call the Datagrid Column Sort function?

      Is it possible to call the same function within the datagrid component that is used when we do an onClick event of the column headers? When you click on the column header, it will sort the column ascending or descending.

      I would like to call that same process after the datagrid is loaded, programmatically. But the same as if the user had clicked the column.

      I have looked at examples in the help that sort the dataprovider array. But if we are able to do this at runtime with a user clicking on the column, it seems that it should be possible since the datagrid is doing a sort on the internal dataprovider object for the datagrid.

      If it is not available now, this functionality should be added.


        • 1. Re: Can we call the Datagrid Column Sort function?
          Level 7
          Yes, you can do what you want by setting the data provider's "sort" property
          to be an instance of class Sort, and putting one or more SortFields inside the

          If you put just a single SortField inside the Sort, then the header field will
          have the up/down triangle indicating the results of the sort, just as if the
          user had clicked on the column. If you put two or more SortFields inside the
          Sort, then there won't be a triangle, because of course it wouldn't make sense
          to put on there.

          Here is a small sample:

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml">

          <mx:Array id="mydata">
          <mx:Object col1="b" col2="p" col3="z" />
          <mx:Object col1="c" col2="r" col3="y" />
          <mx:Object col1="b" col2="q" col3="x" />
          <mx:Object col1="a" col2="q" col3="x" />
          <mx:Object col1="d" col2="q" col3="x" />
          <mx:Object col1="c" col2="q" col3="x" />

          <mx:ArrayCollection id="mycollection" source="{mydata}">
          <!-- This example has field "col2"
          as its primary sort field, and
          field, and field "col1" as its
          secondary sort field.

          If you remove one of these lines,
          so only a single SortField
          exists, you will see a triangle
          in the header of the sorted
          field in the DataGrid.
          <mx:SortField name="col2" />
          <mx:SortField name="col1" />

          <mx:DataGrid id="dg" height="474" dataProvider="{mycollection}" >
          <mx:DataGridColumn headerText="Column 1"
          <mx:DataGridColumn headerText="Column 2"
          <mx:DataGridColumn headerText="Column 3"

          Mike Morearty
          Developer, Flex Builder team
          • 2. Re: Can we call the Datagrid Column Sort function?
            is2 Level 1
            Took your suggestion for the ArrayCollection and put it within the dataProvider for the grid and seems to work just great.

            My source is a webservice which returns an xml list ( xmlAccountsList ). Since I got an error using the ArrayCollection, I tried the XmlListCollection and worked as you suggested. Excellent. I will now try on my customer and vendor browse screens.

            Code for my grid is below for someone to look at in case they need this also.

            Thank you so much Mike.

            <mx:DataGrid id="dgAccounts" x="7" y="7" width="583" height="235">
            <mx:SortField name="AccountNumber" />
            <mx:DataGridColumn headerText="Account" dataField="AccountNumber" width="100" />
            <mx:DataGridColumn headerText="Account Name" dataField="Name" width="300" />
            <mx:DataGridColumn headerText="Account Type" dataField="AccountType" />