5 Replies Latest reply on Dec 28, 2009 7:37 AM by Faser

    How to disable auto sorting in a datagrid

    Faser

      Every time a new item is added to the data provider or some properties are changed, the rows in the datagrid are automatically sorted using the new values.
      How can I prevent this behavior? I tried using collection.disableAutoUpdate(), but in this way the values shown in the datagrid are not updated too.
      I want the datagrid is sorted only when the user click on the columns header, not when the data change.

        • 1. Re: How to disable auto sorting in a datagrid
          Francisc Level 3

          Try sortableColumns=false (as3) or sortableColumns ="false" (mxml) for the DataGrid Control.

          • 2. Re: How to disable auto sorting in a datagrid
            Faser Level 1

            Before updating the values I empty out the sort.fields array and I avoid

            to refresh the collection binded to the datagrid.

             

            collection.sort.fields = ;

            //collection.refresh();}

             

            It seems to work.

            • 3. Re: How to disable auto sorting in a datagrid
              Faser Level 1

              No, sorry I was wrong. It doesn't work and sortableColumns=false prevent the user to click the column header, but when some items in dataprovider change the row in the datagrid are reordered.

              • 4. Re: How to disable auto sorting in a datagrid
                Flex harUI Adobe Employee

                dataProvider = dataProvider.toArray() will lock the current sort order.

                 

                Alex Harui

                Flex SDK Developer

                Adobe Systems Inc.

                Blog: http://blogs.adobe.com/aharui

                • 5. Re: How to disable auto sorting in a datagrid
                  Faser Level 1

                  Thank you Alex, but in this way the datagrid is no more updated when I  change the collection.

                   

                  dataGrid.dataProvider = myCollection.toArray();

                   

                   

                  So, the only way I found to prevent auto-sorting is to add a a new  property to the objects in the collection. This field store the original  rowIndex and I set the datagrid to sort using this new field.

                   

                   

                  public function sortByItemIndex():void{

                   

                              var itemIndexSortField:SortField = new SortField();

                   

                                  itemIndexSortField.name = "itemIndex";

                   

                                  itemIndexSortField.numeric = true;

                   

                   

                              var mySort:Sort = new Sort();

                   

                                  mySort.fields = [itemIndexSortField];

                   

                   

                                  collection.sort = mySort;

                   

                                  collection.refresh();

                   

                          }

                   

                   

                   

                  public function setItemIndex():void {

                   

                              for each(var order:Order in collection){

                   

                                  order.itemIndex = collection.getItemIndex(order);

                   

                              }

                   

                          }