1 Reply Latest reply on Aug 14, 2009 3:48 AM by Madhav Subedi

    Help needed to add an image to a datagrid cell in actionscript

    xandesign

      Morning all,

       

      I am still quite new to flex development and I have an application which uses xml to populate a datagrid. One of the row columns should display a small image but I don't know how to do that.

       

      Can anyone show me how to add an image to a datagrid cell in actionscript?

       

      I've added a sample of the code I have written already below. Any help would be much appreciated.

       

      Thanks in advance,

       

      Xander

       

       

      My XM

      <?xml version="1.0" encoding="UTF-8"?>
      <dataset>
      <modules>
      <module id="1">
      <icon>assets/sample_image1.png</icon>
      <key>core</key>
      <name>Core</name>
      <description>Description of module</description>
      <installed>Wednesday, 24th June 2009 @ 15:59 UK</installed>
      </module>
      <module id="2">
      <icon>assets/sample_image2.png</icon>
      <key>webproject</key>
      <name>Web Project</name>
      <description>Description of module</description>
      <installed>Wednesday, 24th June 2009 @ 17:32 UK</installed>
      </module>
      </modules>
      </dataset>

       

       

      My Actionscript

      private function dataSetHandler(event:Event):void {
          var ds:XML = new XML(event.target.data);
          var rows:XMLList = ds.elements('modules').elements('module') as XMLList;
          var columns:Array = new Array();
         
          for (var i:int=0; i<rows[0].elements().length(); i++) {
              var column:DataGridColumn = new DataGridColumn();
              var tag:String = rows.*[i].name();

       

              column.headerText = rows.*[i].name();

              column.dataField = rows.*[i].name();
                     
              if (tag == 'icon') {
                  var img:Image = new Image();
                 
                  img.id = "iconpath";
                  img.width = 23;
                  img.height = 20;
                  img.source = rows.*[i].name();
                 
                 column.itemRenderer = img;   <-- this line shows as an error when I try to compile
              }
             
              columns[i] = column;
          }
         
          mydatagrid.columns = columns;
          mydatagrid.dataProvider = rows;
      }