3 Replies Latest reply on Mar 13, 2013 10:03 AM by Gug88

    Moving datagrid rows up and down

    iamcootis Level 1

      I have a datagrid and I want to be able to move one row at a time, up or down. What would be the best way to make this happen? Is there a built in function for this?


      I currently want to be able to select a row and then push an up or down button to move it. Could I just take the dataProvider and get the items current index and do an addItemAt(new index);


      Thanks for your help.

        • 1. Re: Moving datagrid rows up and down
          VRPDeveloper Level 3

          There is no built in function for this... Make copy of your data provider You have to fetch row number and then copy the new dataprovider till that row number and then change the data... Do not forget to refresh data provider,,



          Hope you got my point!!!


          If this post answers your question or helps, please kindly mark it as such.

          • 2. Re: Moving datagrid rows up and down
            ShardulSingh Level 3




            I have written this code simply for the up. You can write similarly for the down. Pls check this code.Let  me know if you have any issue.




            <?xml version="1.0"?>
            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" >
                    import mx.controls.Alert;
                    import mx.collections.ArrayCollection;
                    private var ac : ArrayCollection = new ArrayCollection([
                    {name : 'Smith',age : '45'},
                    {name : 'Jake',age : '44'},
                    {name : 'Carls',age : '43'},
                    {name : 'Robert',age : '33'},
                    public function up():void
                        if(dg.selectedIndex == -1)
                            Alert.show("Select the row which you want to up.");
                        if(dg.selectedIndex == 0)return;
                        var selectedRowInx : Number = dg.selectedIndex;
                        var itemToShift : Object = ac.getItemAt(selectedRowInx) as Object;


               <mx:DataGrid id="dg" width="350" dataProvider="{ac}">
                     <mx:DataGridColumn dataField="name" />
                     <mx:DataGridColumn dataField="age" />


            <mx:Button label="Up the Row" click="up()"/>



            • 3. Re: Moving datagrid rows up and down

              Hey i know this is an old post but i was using it and was having some problems when it comes to move down ...


              Wouldn't the code be similar just having a difference in this line..




              Cheers can somebody confirm please(cause its not working for me