3 Replies Latest reply on Jun 30, 2006 10:09 AM by cjwprostar.

    Adding an itemRenderer to a DataGrid at runtime

    cjwprostar. Level 1
      I'm trying to add an itemRenderer(ComboBox) to certain columns in a DataGrid depending on the value of a string from my data. I need to set the values of the ComboBoxes with data from another data source and set certain styles to the ComboBoxes. This is what I have so far.

      private function PopulateProjectDataGrid():void{
      var fieldList:XMLList=projectDataRequest.lastResult.child('project1').child('field');
      var columns:Array=projectDataGrid.columns;
      for(var i:int=0;i<fieldList.length();++i){
      var newColumn:DataGridColumn=new DataGridColumn("column");
      newColumn.headerText=fieldList .child("fieldName");
      newColumn.dataField="field"+(i+1);
      if(fieldList
      .child('fieldType')=="List"){
      newColumn.itemRenderer=new ClassFactory(ComboBox);
      }
      columns.push(newColumn);
      projectDataGrid.columns=columns;
      }
      }

      Thanks,
      Chris
        • 1. Re: Adding an itemRenderer to a DataGrid at runtime
          cjwprostar. Level 1
          Ok, after looking more into this, I understand that the ClassFactory creates a ComboBox class for each row, but I still don't know how to access the properties of each instance of the ComboBoxes. I see there is a ClassFactory.properties property, but I don't know if this is the right area/how to add properties to it.

          Thanks,
          Chris
          • 2. Re: Adding an itemRenderer to a DataGrid at runtime
            inlineblue Level 1
            I haven't seen a way to get at the individual item renderer instances. However, perhaps your problem can be turned around. Why not create a new renderer by inheriting from ComboBox and then have it set its own styles depending on the data it gets? This is a more OOP approach anyway.
            • 3. Re: Adding an itemRenderer to a DataGrid at runtime
              cjwprostar. Level 1
              Yay! I dug in the ClassFactory source file and figured out that I could use an object array to set the properties.

              private function PopulateProjectDataGrid():void{
              var fieldList:XMLList=projectDataRequest.lastResult.child('project1').child('field');
              var columns:Array=projectDataGrid.columns;
              var cf:ClassFactory=new ClassFactory(ComboBox);
              var ob:Object=new Object();
              for(var i:int=0;i<fieldList.length();++i){
              var newColumn:DataGridColumn=new DataGridColumn("column");
              newColumn.headerText=fieldList .child("fieldName");
              newColumn.dataField="field"+(i+1);
              if(fieldList
              .child('fieldType')=="List"){
              ob["alpha"]=0.1;
              cf.properties=ob;
              newColumn.itemRenderer=cf;
              }
              columns.push(newColumn);
              projectDataGrid.columns=columns;
              }
              }