Archive for November, 2008

Create columns on the fly for advanced datgrid in (actionscript)as /flex

Write event listner for ur datagrid component, when ever your dataprovider(data provider must be bindable) changes it calls collection change event,

example:-

//genricDG is my datagridgenricDG.addEventListener(CollectionEvent.COLLECTION_CHANGE, columnListener);

 private function columnListener(event:CollectionEvent):void
  {
   var cols:Array = new Array();//genricDG.columns;
  if(model.GridColumnName)//array of column name
  {
  var oColumnDef:Object;
      var dgc:AdvancedDataGridColumn;
      var iTotalDGWidth:int = 0;
      if(model.GridColumnName.length != 0)
      {
       dgc=new AdvancedDataGridColumn();
       dgc.dataField=”tno”;
   dgc.headerText=”S. No.”
   cols.push(dgc);
    
       for (var i:int=0;i<model.GridColumnName.length;i++) 
       {
     
    dgc.dataField=model.GridColumnName[i];
    dgc.headerText=model.GridColumnName[i];
    //dgc.setStyle(“textAlign”, model.controls[j].TextAlign );
    dgc.showDataTips=true;
    cols.push(dgc);
   }

   }    
      genricDG.columns=cols;                                                 
  }

   else
      {
      cols=new Array();
      genricDG.columns=null;
      genricDG.columns=cols;
      }
    }
           
  }

Datagrid cell validation in Action script

take datagrid with property editable=”true”

 public var EditingRowIndex:int;private function testItemEditEnd(event:AdvancedDataGridEvent):void
{
EditingcolIndex=event.columnIndex;
//Previous column index
ComparecolIndex=event.columnIndex -1;
EditingRowIndex=event.rowIndex;
colReference=genricDG.columns[EditingcolIndex];
//Gives column name for Comapre Column
comReference=genricDG.columns[ComparecolIndex][“dataField”];
var CompareValue:int=genricDG.dataProvider[EditingRowIndex][comReference];
var EditedValue:int = parseInt(TextInput(event.currentTarget.itemEditorInstance).text);
 if(CompareValue > EditedValue)
{

//no validation coz satisfies the condition
//total.text =String(parseInt(total.text) + EditedValue);

}

 else
{

//PREVENT DEFAUL WILL PERSIST VALUE OF DATAGRID CELL
event.preventDefault();
//VALIDATION AFTER CONVERTIN itemEditorInstance INTO TextIput
TextInput(event.currentTarget.itemEditorInstance).errorString =
“Value Must be Less than or qual o” + comReference;

}

}