26 Replies Latest reply on Jul 6, 2011 5:56 AM by gajanan hiroji

    Adding Dynamic column to datagrid at the begining of datagrid

    gajanan hiroji

      Hello friends,

       

      There is one datagrid which is static. and i m adding one dynamic column. it is get added to the end of the static datagrid

       

      but I want to add dynamic column at the begning of the datagrid can anybody help me on this.

       

      Thanking you,

       

      Regards,

      gajanan

        • 1. Re: Adding Dynamic column to datagrid at the begining of datagrid
          rstanvir Level 1

          try this

           

          var desiredColumnIndex: Number = 0;

          dataGrid.columns.addItemAt(gridColumn,desiredColumnIndex)

           

          hope it will resolve your problem.

          1 person found this helpful
          • 2. Re: Adding Dynamic column to datagrid at the begining of datagrid
            gajanan hiroji Level 1

            Thanks for your reply Rstanvir,

             

            i done that but it is giving error this is error see  "Error #1006: addItemAt is not a function.".

             

            thanking you

             

            regards,

            gajanan

            • 3. Re: Adding Dynamic column to datagrid at the begining of datagrid
              rstanvir Level 1

              you have to set some IList implemented object (i.e. arraycollection) in dataGrid.columns first otherwise it will remain null and you will get error like that you got

               

              for example, initially create a datagrid like this:

               

              var gridColumns : ArrayCollection = new ArrayCollection();

              var gridColumn : GridColumn;

               

              for each(var item in someList)

              {

                gridColumn = new GridColumn();

                gridColumns.addItem(gridColumn);

              }

               

              dataGrid.columns = gridColumns;

               

              when adding new dynamic gridcolumn use:

               

              var desiredIndex : Number = 0;

              gridColumns.addItemAt(new GridColumn(), desiredIndex); 

               

              the above will be enough if you have data binding between dataGrid.columns and gridColumns

              otherwise

               

              gridColumns.addItemAt(new GridColumn(), desiredIndex);

              dataGrid.columns = gridColumns;

              or

              dataGrid.columns.addItemAt(new GridColumn(), desiredIndex);

               

               

              thanks

              • 4. Re: Adding Dynamic column to datagrid at the begining of datagrid
                gajanan hiroji Level 1

                Thanks Rstanvir,

                 

                I have a static datagrid to which  i want to add a column to the begning of the datagrid. that is the column is dynamic. in flex 4

                 

                thanking you

                 

                regards,

                gajanan

                • 5. Re: Adding Dynamic column to datagrid at the begining of datagrid
                  rstanvir Level 1

                  can you post your code snippet where you added your datagrid ?

                  • 6. Re: Adding Dynamic column to datagrid at the begining of datagrid
                    gajanan hiroji Level 1

                    see this is my datagrid id is datagridfeeamount  first i m assigning value to datagrid then i have add the colunm

                     

                                            datagridfeeamount.dataProvider=event.result.rows.row;
                                            var dt:DataGridColumn=new DataGridColumn("");
                                            dt.headerRenderer=new ClassFactory(CheckBoxHeaderRenderer);
                                            dt.dataField="firstName";
                                            var col:Array=datagridfeeamount.columns;
                                            dt.editable=false;
                                            dt.sortable=false;
                                            dt.itemRenderer=new ClassFactory(CheckBoxRenderer);
                                            dt.width=20;
                                            col.push(dt);
                                            datagridfeeamount.columns=col;

                     

                    But i have to add this column to the begning of the datagrid

                    • 7. Re: Adding Dynamic column to datagrid at the begining of datagrid
                      rstanvir Level 1

                                              datagridfeeamount.dataProvider=new ArrayCollection(event.result.rows.row);


                                              var dt:DataGridColumn=new DataGridColumn("");
                                              dt.headerRenderer=new ClassFactory(CheckBoxHeaderRenderer);
                                              dt.dataField="firstName";

                       

                                              var col:ArrayCollection=datagridfeeamount.columns;


                                              dt.editable=false;
                                              dt.sortable=false;
                                              dt.itemRenderer=new ClassFactory(CheckBoxRenderer);
                                              dt.width=20;
                                              col.addItemAt(dt,0);
                                              datagridfeeamount.columns=col;

                       

                      it is wise to use ArrayCollection for a dataProvider .

                      hope it will help

                       

                      rstanvir

                       

                       

                      marked as answer if it resolves the problem.

                      also it gives a peace in mind knowing that you help to resolve a problem in this problematic world.

                      1 person found this helpful
                      • 8. Re: Adding Dynamic column to datagrid at the begining of datagrid
                        gajanan hiroji Level 1

                        thanks for your reply Rstanvir,

                         

                        This i done and but still i m getting error

                         

                                                 datagridfeeamount.dataProvider=ArrayCollection( event.result.rows.row);
                                                var dt:DataGridColumn=new DataGridColumn("");
                                                dt.headerRenderer=new ClassFactory(CheckBoxHeaderRenderer);
                                                dt.dataField="firstName";
                                                var col:ArrayCollection=ArrayCollection(datagridfeeamount.columns);
                                                dt.editable=false;
                                                dt.sortable=false;
                                                dt.itemRenderer=new ClassFactory(CheckBoxRenderer);
                                                dt.width=20;
                                                col.addItemAt(dt,0);
                                                //col.push(dt);
                                                //var desiredColumnIndex: Number = 0;
                                                //datagridfeeamount.columns.(col,desiredColumnIndex)
                                                 datagridfeeamount.columns=Array(col);

                         

                        and the error is

                         

                         

                        Error #1034: Type Coercion failed: cannot convert []@b737d99 to mx.collections.ArrayCollection.

                        • 9. Re: Adding Dynamic column to datagrid at the begining of datagrid
                          rstanvir Level 1
                            datagridfeeamount.dataProvider = new ArrayCollection( event.result.rows.row);


                                                  var dt:DataGridColumn=new DataGridColumn("");
                                                  dt.headerRenderer=new ClassFactory(CheckBoxHeaderRenderer);
                                                  dt.dataField="firstName";

                           

                                                  var col:ArrayCollection=datagridfeeamount.columns;


                                                  dt.editable=false;
                                                  dt.sortable=false;
                                                  dt.itemRenderer=new ClassFactory(CheckBoxRenderer);
                                                  dt.width=20;
                                                  col.addItemAt(dt,0);
                                                  //col.push(dt);
                                                  //var desiredColumnIndex: Number = 0;
                                                  //datagridfeeamount.columns.(col,desiredColumnIndex)

                           

                                                  datagridfeeamount.columns = col;

                           

                          sorry, you mistakenly ommitted the new keyword.

                          • 10. Re: Adding Dynamic column to datagrid at the begining of datagrid
                            gajanan hiroji Level 1

                            thanks


                            where am i suppose to put new keyword. u have not specified.

                             

                            Regards,

                            gajanan

                            • 11. Re: Adding Dynamic column to datagrid at the begining of datagrid
                              rstanvir Level 1

                              datagridfeeamount.dataProvider = new ArrayCollection( event.result.rows.row);


                                                      var dt:DataGridColumn=new DataGridColumn("");
                                                      dt.headerRenderer=new ClassFactory(CheckBoxHeaderRenderer);
                                                      dt.dataField="firstName";

                               

                                                      var col:ArrayCollection=datagridfeeamount.columns;


                                                      dt.editable=false;
                                                      dt.sortable=false;
                                                      dt.itemRenderer=new ClassFactory(CheckBoxRenderer);
                                                      dt.width=20;
                                                      col.addItemAt(dt,0);
                                                      //col.push(dt);
                                                      //var desiredColumnIndex: Number = 0;
                                                      //datagridfeeamount.columns.(col,desiredColumnIndex)

                               

                                                      datagridfeeamount.columns = col;

                               

                              hope you can see it now.

                              • 12. Re: Adding Dynamic column to datagrid at the begining of datagrid
                                gajanan hiroji Level 1

                                hi Rstanvir,

                                 

                                I m getting your point but when we are taking the datagrid colunm in arraycollection it is giving error as " Implicit coercion of a value of type Array to an unrelated type mx.collections:ArrayCollection." and when i typecast to array as arraycollection it working but while run time it is giving error

                                 

                                see here when u do this      "var col:ArrayCollection=datagridfeeamount.columns" this is the error "Implicit coercion of a value of type Array to an unrelated type mx.collections:ArrayCollection."  

                                 

                                and when i do this var col:ArrayCollection=Arraycollection(datagridfeeamount.columns); it is giving error at run time so can i help me on this now

                                 

                                thanks

                                 

                                Regards,

                                gajanan

                                • 13. Re: Adding Dynamic column to datagrid at the begining of datagrid
                                  kokorito Level 4

                                  datagridfeeamount.dataProvider = new ArrayCollection( event.result.rows.row);

                                   

                                   

                                  the word "new", after the = was supposed to be red and bigger

                                  • 14. Re: Adding Dynamic column to datagrid at the begining of datagrid
                                    gajanan hiroji Level 1

                                    hi Rstanvir

                                     

                                    tat is working fine but when i m assigning all the columns of datagrid to arraycollection it is giving error it is saying we have take as array only and add the colunm using push method

                                    but if i take datagrid columns to arraycollection it is giving an error.

                                     

                                    Regards,

                                    Gajanan

                                    • 15. Re: Adding Dynamic column to datagrid at the begining of datagrid
                                      kokorito Level 4

                                      ah, sorry. Its the col array not the dataprovider

                                      can you use an array for the

                                      columns, then to add a new column at the front use col.unshift(newColumn);

                                       

                                      .push() adds an element to the end of an array

                                      .unshift() adds it at the beginning

                                      • 16. Re: Adding Dynamic column to datagrid at the begining of datagrid
                                        gajanan hiroji Level 1

                                        hi Rstanvir,

                                         

                                        unshift not function for array in flex 4 i dont aboubt the flex 3

                                        but when i m doing col.unshift(newcolunm)

                                        it is giving error like this "Error #1006: unshif is not a function."

                                         

                                        Regards,

                                        Gajanan

                                        • 17. Re: Adding Dynamic column to datagrid at the begining of datagrid
                                          kokorito Level 4

                                          what? yes it is. I use Flex 4.5

                                           

                                          var a:Array = new Array("second","third");

                                          a.unshift("first");
                                          trace(a);

                                           

                                          //first,second,third

                                           

                                           

                                          you have changed the col back to an array rather than an arraycollection?

                                          columns go in an array, use unshift()

                                          dataprovider goes in an arraycollection, use addElementAt()

                                           

                                           

                                           

                                          btw, its unshift

                                          not unshif (you missed the t)

                                          • 18. Re: Adding Dynamic column to datagrid at the begining of datagrid
                                            gajanan hiroji Level 1

                                            thanks for your reply

                                             

                                            see this what i done

                                             

                                                                    datagridfeeamount.dataProvider=ArrayCollection( event.result.rows.row);
                                                                    var dt:DataGridColumn=new DataGridColumn("");
                                                                    dt.headerRenderer=new ClassFactory(CheckBoxHeaderRenderer);
                                                                    dt.dataField="firstName";
                                                                    var col:Array=new Array(datagridfeeamount.columns);
                                                                    dt.editable=false;
                                                                    dt.sortable=false;
                                                                    dt.itemRenderer=new ClassFactory(CheckBoxRenderer);
                                                                    dt.width=20;
                                                                    col.unshift(dt);
                                                                    datagridfeeamount.columns=col;

                                             

                                             

                                            but it is saying as unshift is not an function

                                             

                                            regards,

                                            gajanan

                                            • 19. Re: Adding Dynamic column to datagrid at the begining of datagrid
                                              kokorito Level 4

                                              datagridfeeamount.dataProvider=ArrayCollection( event.result.rows.row);
                                              var dt:DataGridColumn=new DataGridColumn("");
                                              dt.headerRenderer=new ClassFactory(CheckBoxHeaderRenderer);
                                              dt.dataField="firstName";
                                                                    
                                              dt.editable=false;
                                              dt.sortable=false;
                                              dt.itemRenderer=new ClassFactory(CheckBoxRenderer);
                                              dt.width=20;

                                              datagridfeeamount.columns.addItemAt(dt,0);

                                               

                                              try that then

                                              • 20. Re: Adding Dynamic column to datagrid at the begining of datagrid
                                                gajanan hiroji Level 1

                                                Hi,

                                                 

                                                I done that but it is not adding the column only

                                                 

                                                Regards,

                                                gajanan

                                                • 21. Re: Adding Dynamic column to datagrid at the begining of datagrid
                                                  gajanan hiroji Level 1

                                                  hello

                                                   

                                                  i done this also "datagridfeeamount.columns. unshift(dt);" it is not giving error but it is not adding column to the datagrid.

                                                   

                                                  Regards,

                                                  Gajanan

                                                  • 22. Re: Adding Dynamic column to datagrid at the begining of datagrid
                                                    kokorito Level 4

                                                    are you adding the column after the grid is displayed?

                                                     

                                                    maybe try doing a .validateNow() to get the grid to redraw itself with the new column

                                                    • 23. Re: Adding Dynamic column to datagrid at the begining of datagrid
                                                      gajanan hiroji Level 1

                                                      yes i m adding grid after displaying.

                                                       

                                                      i done this

                                                       

                                                      datagridfeeamount.dataProvider=ArrayCollection( event.result.rows.row);
                                                                              var dt:DataGridColumn=new DataGridColumn("");
                                                                              dt.headerRenderer=new ClassFactory(CheckBoxHeaderRenderer);
                                                                              dt.dataField="firstName";
                                                                      
                                                                              var storedata1:Array=new Array()
                                                                              dt.editable=false;
                                                                              dt.sortable=false;
                                                                              dt.itemRenderer=new ClassFactory(CheckBoxRenderer);
                                                                              dt.width=20;
                                                                              datagridfeeamount.columns.unshift(dt,0);
                                                                              datagridfeeamount.allowMultipleSelection=true;
                                                                              datagridfeeamount.validateNow();

                                                       

                                                      i done the validation also but it is not redrawing.

                                                       

                                                      regards,

                                                      gajanan

                                                      • 24. Re: Adding Dynamic column to datagrid at the begining of datagrid
                                                        kokorito Level 4

                                                        umm, try invalidateDisplayList() first

                                                         

                                                        also try adding a break point and running the debugger. Check that the column is actually being added to the grid

                                                        • 25. Re: Adding Dynamic column to datagrid at the begining of datagrid
                                                          gajanan hiroji Level 1

                                                          Thanks

                                                           

                                                          when i done that it is not giving any error means colunm is added to datagrid but it is not showing

                                                          means datagrid is not redrawing that is the problem

                                                           

                                                          regards,

                                                          Gajanan

                                                          • 26. Re: Adding Dynamic column to datagrid at the begining of datagrid
                                                            gajanan hiroji Level 1

                                                            Thanks to everybody.

                                                             

                                                            finally i got the things to work using following code.

                                                             

                                                            private  function SwapGridColumns (DG:DataGrid, Col1:Number, Col2:Number):void
                                                                        {
                                                                            var i:Number; // Counter
                                                                            var DP:ArrayCollection =ArrayCollection( DG.dataProvider);     //DataProvider
                                                                            var Widths:Array = new Array ();     // the widths of the columns
                                                                            var Columns:Array = new Array ();    // the column names
                                                                            var Visibility:Array = new Array (); // visibility of columns
                                                                            var HRenderer:Array = new Array ();   // Header Renderer of columns
                                                                            var IRenderer:Array = new Array();
                                                                            var datastore:Array = new Array ();
                                                                            var Editable:Array = new Array();
                                                                            var DFields:Array=new Array();
                                                                            var Sortable:Array = new Array();

                                                             

                                                                            for (i = 0; i<DG.columns.length; i++)
                                                                            {
                                                                                if (i == Col1)
                                                                                {
                                                                                    Columns.push(DG.columns[Col2].headerText);
                                                                                    Widths.push(DG.columns[Col2].width);
                                                                                    Visibility.push(DG.columns[Col2].visible);
                                                                                    HRenderer.push(DG.columns[Col2].headerRenderer);
                                                                                    IRenderer.push(DG.columns[Col2].itemRenderer);
                                                                                    Editable.push(DG.columns[Col2].editable);
                                                                                    Sortable.push(DG.columns[Col2].sortable);
                                                                                    DFields.push(DG.columns[Col2].dataField);
                                                                                }
                                                                                else if (i == Col2)
                                                                                {
                                                                                    Columns.push(DG.columns[Col1].headerText);
                                                                                    Widths.push(DG.columns[Col1].width);
                                                                                    Visibility.push(DG.columns[Col1].visible);
                                                                                    HRenderer.push(DG.columns[Col1].headerRenderer);
                                                                                    IRenderer.push(DG.columns[Col1].itemRenderer);
                                                                                    Editable.push(DG.columns[Col1].editable);
                                                                                    Sortable.push(DG.columns[Col1].sortable);
                                                                                    DFields.push(DG.columns[Col1].dataField);
                                                                                }
                                                                                else
                                                                                {
                                                                                    Columns.push(DG.columns[i].headerText);
                                                                                    Widths.push(DG.columns[i].width);

                                                                                    HRenderer.push(DG.columns[i].headerRenderer);
                                                                                    IRenderer.push(DG.columns[i].itemRenderer);
                                                                                    Editable.push(DG.columns[i].editable);
                                                                                    Sortable.push(DG.columns[i].sortable);
                                                                                    DFields.push(DG.columns[i].dataField);
                                                                                }
                                                                            }
                                                                           
                                                                                    
                                                                            // Reset the widths
                                                                            var col:Array
                                                                            for (i = 0; i < Columns.length; i++)
                                                                            {
                                                                                var DGC:DataGridColumn = new DataGridColumn(Columns[i]);
                                                                                DGC.width = Widths[i];
                                                                                DGC.visible = Visibility[i];

                                                                                DGC.headerRenderer = HRenderer[i];
                                                                                DGC.itemRenderer = IRenderer[i];
                                                                                DGC.dataField=DFields[i];
                                                                                DGC.sortable=Sortable[i];
                                                                                DGC.editable=Editable[i];
                                                                                datastore.push(DGC);
                                                                            }
                                                                            DG.columns=datastore;
                                                                            // Load DP
                                                                                 
                                                                            DG.dataProvider = DP;
                                                                        }

                                                             

                                                             

                                                            Regards,

                                                            gajanan