0 Replies Latest reply on Aug 8, 2011 5:34 AM by flexasdf

    Dynamically resizing colums width of DataGrid

    flexasdf

      Hi, i've a problem with resizing columns in datagrid. I need to auto  resize columns depending on data - it works and also not work. Example  will be better:

       


      I've a custom component MyDataGrid which looks like:

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:DataGrid xmlns:mx="http://www.adobe.com/2006/mxml">
           <mx:Script>
               <![CDATA[      

       

                  override protected function adjustListContent(unscaledWidth:Number = -1, unscaledHeight:Number = -1):void {
                       super.adjustListContent(unscaledWidth, unscaledHeight);

                       autoResizeColumns();
                  }
                                  
                   public function autoResizeColumns ():void {
                       // i am only resizing columns here, lets say tis way:
                       for(var i:int=0; i++; i<columns.length) {
                          columns[i] = 222;           
                       }
                   }
               ]]>
           </mx:Script>           
      </mx:DataGrid>

       

      But when i create MyDataGrid in my application like below:

       

                    <components:DataGrid horizontalScrollPolicy="auto" width="800" dataProvider="{items}" id="myDg">
                           <components:columns>
                               <mx:DataGridColumn headerText="col1" dataField="field1"/>
                               <mx:DataGridColumn headerText="col2" dataField="field2"/>
                               <mx:DataGridColumn headerText="col3" dataField="field3"/>
                           </components:columns>
                       </components:DataGrid>

       

      resizing works, but after that columns are not resizable, not sortable, and datagrid not scrollable - columns have the width I wanted, but that's all - it looks like every other datagrid behaviour dissappeared. So I commented autoResizeColumns() in overrided adjustListContent() method and tried to make a button which will call autoResizeColumns() method directly:

       

          <mx:Button label="Auto resize columns" id="resizeBtn" click="myDg.autoResizeColumns();"/>
         
      And  this works perfectly. So my question is what i'm doing wrong? I'm  probably resizing columns good way, but invoking it at bad place (in  function adjustListContent). Any advices? Thx.