title: DataGrid column width is not set properly
body: <p>In the below example I has called the OptimizeDataGrid method on Button Click event to resize the Column according to the data. whereas on the First Time of the Button Click event It called the function whereas the Datagrdi Column width alone is not set properly.whereas on second time it is working properly.</p>
  
  <p>For Eg : In the below statement the text value is coming as 55 and widthPadding is comes as 25. but the sum of these two value is not in the dg.columns[col].width.</p>
  
  <p>dg.columns[col].width = text + widthPadding;</p>
  
  <p>But the same is working on the second time of the Button click event. is any help me out to resovle this issue. Thanks in Advance.</p>
  
  <p>
  </p>
  
  <pre><code>&lt;mx:Script&gt;
      &lt;![CDATA[
          import mx.utils.ObjectUtil;
          import mx.collections.ArrayCollection;
          import mx.core.UITextField;
          import mx.events.AdvancedDataGridEvent;
          import mx.binding.utils.BindingUtils;
          import mx.controls.Alert;           
          import mx.controls.AdvancedDataGrid;
          import mx.controls.advancedDataGridClasses.*;
          import mx.controls.DataGrid;
          import mx.controls.dataGridClasses.*;
  
  
          [Bindable]
          private var dpFlat:ArrayCollection = new ArrayCollection([            
            {Region:"aabbCC", Territory:"Central Californiaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
                Territory_Rep:"Joe Smith", Actual:29134, Estimate:30000}, 
            {Region:"AAbbcc", Territory:"Nevada",
                Territory_Rep:"Bethany Pittman", Actual:52888, Estimate:45000}
          ]);
  
  
          private function optimizeDataGrid(dg:Object,widthPadding:uint = 0,heightPadding:uint = 0):void
          {
              if ((!dg is DataGrid))
                  return;
  
  
              var col:uint;
              var colWidth:uint;
              var tf:TextFormat;
              var renderer:UITextField;
              var widths:Array = new Array(dg.columnCount);
              var height:uint = 0;
              var dgCol:Object;
              var text:uint = 0;
  
  
              if (dg.columnCount &gt; 0 &amp;&amp; dg.dataProvider != null)
              {
                  for (col = 0; col &lt; dg.columnCount; ++col)
                      widths[col] = 0;
                  for each (var item:Object in dg.dataProvider)
                  {
                      for (col = 0; col &lt; dg.columnCount; ++col)
                      {
                          renderer = new DataGridItemRenderer();
                          dg.addChild(renderer);
                          dgCol = dg.columns[col];
                          renderer.text = dgCol.itemToLabel(item);
                          widths[col] = Math.max(renderer.measuredWidth, widths[col]);
                          height = Math.max(renderer.measuredHeight, height);
                          dg.removeChild(renderer);
                      }
                  }
  
  
                  for (col = 0; col &lt; dg.columnCount; ++col)
                  {                       
                      dg.addChild(renderer);
                      renderer.text = dg.columns[col].headerText;
                      widths[col] = Math.max(renderer.measuredWidth,widths[col]);
                      dg.removeChild(renderer);
  
                      text = widths[col];
                      dg.columns[col].width = text + widthPadding;
                  }
                  if (height != 0)
                      dg.rowHeight = height + heightPadding;
  
              }
          }
      ]]&gt;
  &lt;/mx:Script&gt;
  
  
  &lt;mx:DataGrid id="myADG"
         dataProvider="{dpFlat}" rowCount="{dpFlat.length}" sortableColumns="false"&gt;       
           &lt;mx:columns&gt;
              &lt;mx:DataGridColumn dataField="Region" /&gt;              
              &lt;mx:DataGridColumn dataField="Territory_Rep" headerText="Territory Rep" /&gt;
               &lt;mx:DataGridColumn dataField="Territory" /&gt;
              &lt;mx:DataGridColumn dataField="Actual" /&gt;
              &lt;mx:DataGridColumn dataField="Estimate" /&gt;
          &lt;/mx:columns&gt;
  </code></pre>
  
  <p></p>
  
  <p></p>
  
  <p></p>