10 Replies Latest reply on Jul 30, 2010 4:40 AM by Aron manager

    Correct time to set dynamic columns size?

    Aron manager

      Hi,

       

      I have to populate a datagrid with columns at runtime. The problem is that I want to resize the columns to a size when they are being added.

       

      I use something like this:

      var acData:Array=new Array();
                     
      //Add columns information. We fill an array of columns to assign it to the datagrid               
      for each ( var obj:StructureEconomicaGastosLineRO in responder.lastResult.objs)
      {
           var dgcData:DataGridColumn=new DataGridColumn(obj.epigraph);
           dgcData.editable=true;
           dgcData.width=100;
           dgcData.dataField="id";
           acData.push(dgcData);
      }
      
      //Datagrid defined in mxml that will show the returned data.
      dgData.columns=acData;
      
      

       

      I do this when the respodner returns the data. The problem is that columns do not resize (to 100 px), do anybody know what's hapenning?

       

      Thanks in advance,

      Aron.

        • 1. Re: Correct time to set dynamic columns size?
          Dajji Level 3

          Where is your code written? Is it in creation complete handler? If not, try moving in creation complete handler.

          • 2. Re: Correct time to set dynamic columns size?
            Aron manager Level 1

            Hi,

             

            The code is written in a function that handles responder RESULT event.

             

            When the creation complete for the form is called, then I ask a remote service for data, and when teh responder gets it, I create the columns and resize them. All is done after everything has been created, and all components initialization life cycle finished (Or this is what I think).

             

            Thanks in advance,

            Aron.

            • 3. Re: Correct time to set dynamic columns size?
              NewFlo

              I think you should maybe take a look at the component live-cycle...

              http://flexcomps.wordpress.com/2008/05/09/flex-component-life-cycle/

              usually the right time to apply changes to mxml components is during the the commitProperties() methode (Therefore you would have to override it). If you don't want to go into that, try to call the invalidateNow() or validateNow()-Methode on your datagrid.

               

              //Datagrid defined in mxml that will show the returned data.
              dgData.columns=acData;

               

              dgData.invalidateNow//Sets flag for the next Component-Live-Cycle-Update

              dgData.validateNow() //Calls the Component-Live-Cycle-Update immediately

               

              I feel most of the time when such a problem happens this is the problem.

               

              Hope it helps!

              All the best, Florian

              • 4. Re: Correct time to set dynamic columns size?
                Aron manager Level 1

                Thanks for the answers,

                 

                The problem is that I'm using horizontalpolicy set to off. If I set it to on/auto everything resizes ok, but I don't want this scroll bar to be shown because I want to control scrolling using another component. So:

                 

                1) Is there anyway to hide scroll bar in a datagrid using horizontalpolicty set to on or auto?

                2) If using horizontalpolicty set to off is there anyway to resize the columns? (Invalidating doesn't work).

                 

                Thanks in advance,

                Aron.

                • 5. Re: Correct time to set dynamic columns size?
                  NewFlo Level 1

                  I just took a look at the article about the component live cycle i postet again and

                  found following paragraph:

                   

                  • Calls the invalidateProperties(), invalidateSize(), and invalidateDisplayList() methods to trigger later calls to the commitProperties(), measure(), or updateDisplayList() methods during the next render event.

                  invalidateProperties() marks a component so that its commitProperties() method gets called during a later screen update.

                  invalidateSize () Marks a component so that its measure() method gets called during a later screen update.

                  invalidateDisplayList () Marks a component so that its updateDisplayList() method gets called during a later screen update.

                  The only exception to this rule is that Flex does not call the measure() method when the user sets the height and width of the component

                   

                  So maybe one more thing you can try is to call the invalidateSize() of validateSize()-Methode of the datagrid or column component to trigger the measure() methode of the uicomponent.

                   

                  all the best florian

                  • 6. Re: Correct time to set dynamic columns size?
                    Aron manager Level 1

                    Hi,

                     

                    I really apreciate your help, but it still doesn't work. I was reading the lyfe cycle document at the time you posted and tried exactly what you said, this is the code:

                     

                    public function StructureEconomicaLinesRetrievingRemoteDataResultHandler(event:ResultEvent) : void
                    {
                      responder.removeEventListener(ResultEvent.RESULT,StructureEconomicaLinesRetrievingRemoteDataResultHandler);
                      responder.removeEventListener(FaultEvent.FAULT,StructureLinesRetrievingRemoteDataFault);
                                   
                      //Add Columns to data datagrid and Totals 
                      var acData:Array=new Array();
                      var mainDGC:DataGridColumn=dgData.columns[0];
                      dgData.columns[0].width=100;
                      acData.push(mainDGC);
                                   
                                   
                      for each ( var obj:StructureEconomicaGastosLineRO in responder.lastResult.objs)
                      {
                           var dgcData:DataGridColumn=new DataGridColumn(obj.epigraph);
                           dgcData.editable=true;
                           dgcData.dataField="id";
                           dgcData.width=200;
                           acData.push(dgcData);
                      }
                                   
                      dgData.columns=acData;
                                   
                      invalidateProperties();
                      invalidateSize();
                      invalidateDisplayList();
                    }
                    

                     

                    It still doesn't resize columns correctly when horizontal scroll policy is set to off. I think when it is off the component is rejecting changes to the columns width? :S.

                     

                    Thanks in advance,

                    Aron.

                    • 7. Re: Correct time to set dynamic columns size?
                      NewFlo Level 1

                      hmm. That setting the horizontalScrollPolicy to off kills the resizing of the component would seem odd to me.

                      I haven't been working with the component-live-cycle for long now, but i also had some troubles latly

                      when i removed some items from a tilelist (scrolling was) on and after using invalidateNow() the scrollbar

                      didn't shrink as expected. But this and similar problems were solved by calling the direct validateX() calls.

                      But i guess you have tried that too...

                       

                      You tried calling the methodes directly on the components to?

                      like

                      dgData.validateSize();
                      dgData.validateNow();

                      or maybe on the colums?

                       

                       

                      Otherwise i have no idea what else could cause the problem.

                       

                      Good luck!

                      • 8. Re: Correct time to set dynamic columns size?
                        Aron manager Level 1

                        Well,

                         

                        I have called validate/invadiateXXX on the datagrid directly and nothing, this methods can't be called on columns. I am a bit dissapointed about this .

                         

                        Thanks for the help,

                        Aron.

                        • 9. Re: Correct time to set dynamic columns size?
                          Aron manager Level 1

                          Hi!,

                           

                          A bit more of information.

                           

                          i have added a button to the form. And if I wait for the columns to display, and press the button (where I change width of a column) it makes the change :S.

                           

                          Does this makes someone guess what's hapenning? I have been following sources an it seems setting columns, invalidates the datagrid and sends a columnsChanged event.

                           

                          Thanks in advance,

                          Aron.

                          • 10. Re: Correct time to set dynamic columns size?
                            Aron manager Level 1

                            Hi,

                             

                            After a couple of hours I have came across this: http://junleashed.wordpress.com/2008/07/10/flex-datagridcolumn-width-management/

                             

                            It states what I first supposed, th eproblem is the scrollpolicy. When you set it to off, widths of columns are overwritten to make all the columns fit in the datagrid client area, this, for me is something I don't like.

                             

                            My aim is to have another control controlling scrolling for the datagrid columns, so this breaks my frist aproach.

                             

                            Knowing this, do anybody know what event should I hear to know that all the resizings where done by the datagrid, and then recalculate my datagridcolumns with myself?.

                             

                            Talking about other things, How can I move the first column shown in the datagrid (as the scrollbar does when you drag it)?.

                             

                            I hope this thread helps someone with same problem,

                            Thanks in advance,

                            Aron.