1 Reply Latest reply on Jan 4, 2011 12:04 PM by Flex harUI

    DataGrid column width is not set properly



              In the below example I has called the OptimizeDataGrid method on Button Click event to resize the Column according to the data. First time of the button click event it called the function whereas the Datagrid Column width alone is not set properly.whereas on second time it is working properly.


      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.


      dg.columns[col].width = text + widthPadding;


      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.



      <?xml version="1.0"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
          width="100%" height="100%" >


                  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.*;
                  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))


                      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 > 0 && dg.dataProvider != null)
                          for (col = 0; col < dg.columnCount; ++col)
                              widths[col] = 0;
                          for each (var item:Object in dg.dataProvider)
                              for (col = 0; col < dg.columnCount; ++col)
                                  renderer = new DataGridItemRenderer();
                                  dgCol = dg.columns[col];
                                  renderer.text = dgCol.itemToLabel(item);
                                  widths[col] = Math.max(renderer.measuredWidth, widths[col]);
                                  height = Math.max(renderer.measuredHeight, height);


                          for (col = 0; col < dg.columnCount; ++col)
                              renderer.text = dg.columns[col].headerText;
                              widths[col] = Math.max(renderer.measuredWidth,widths[col]);
                              text = widths[col];
                              dg.columns[col].width = text + widthPadding;
                          if (height != 0)
                              dg.rowHeight = height + heightPadding;


          <mx:DataGrid id="myADG"
                 dataProvider="{dpFlat}" rowCount="{dpFlat.length}" sortableColumns="false">       
                      <mx:DataGridColumn dataField="Region" />              
                      <mx:DataGridColumn dataField="Territory_Rep" headerText="Territory Rep" />
                       <mx:DataGridColumn dataField="Territory" />
                      <mx:DataGridColumn dataField="Actual" />
                      <mx:DataGridColumn dataField="Estimate" />
         <mx:Button label="click" id="click" click="optimizeDataGrid(myADG,25,5);"/>