4 Replies Latest reply on Mar 9, 2011 9:15 AM by UbuntuPenguin

    How do I dynamically build a grid for printing?

    Jerry62712 Level 1

      I'm trying to print an advancedatagrid.  It would seem to have more control over the output, I need to dynamically create a grid and populate it and format it.  To do this I have this code:

       

                 private function printCaseNotesAS1():void
                  {
                      var flexPrint1:FlexPrintJob = new FlexPrintJob();
                      var myGridPrint:PrintAdvancedDataGrid = new PrintAdvancedDataGrid();
                      var numNotes:int = model.dpCASENOTES.length;
                      if (numNotes > 0)
                      {
      //
      // get the data (model) and put in a collection with the columns I want
      // 
                         var dataSource:ArrayCollection = new ArrayCollection();
                          for (var i:int = 0; i < numNotes; i++)
                          {
                              var descHolder:Object = new Object();
                              descHolder.Description = 
                                    (model.dpCASENOTES[i] as CaseNotesVO).shortDescription.toString();
                              descHolder.NoteNum = "";
                              descHolder.Notes = "";
                              descHolder.UpdatedBy = "";
                              descHolder.PostedDate = "";
                              dataSource.addItem(descHolder);
                              var dataHolder:Object = new Object();
                              dataHolder.Description = "";
                              dataHolder.NoteNum = (i+1).toString();
                              dataHolder.Notes = (model.dpCASENOTES[i] as CaseNotesVO).notes.toString();
                              dataHolder.UpdatedBy = "";
                              dataHolder.PostedDate = "";
                              dataSource.addItem(dataHolder);
                              var footHolder:Object = new Object();
                              footHolder.Description = "";
                              footHolder.NoteNum = "";
                              footHolder.Notes = "";
                              footHolder.UpdatedBy = (model.dpCASENOTES[i] as CaseNotesVO).updatedBy.toString();
                              footHolder.PostedDate = (model.dpCASENOTES[i] as CaseNotesVO).postTs.toString();
                              dataSource.addItem(footHolder);
                          }
      //
      // add a dynamic grid temporarily, point the datasource to the above array
      //
                          if (flexPrint1.start())
                          {
                              FlexGlobals.topLevelApplication.addElement(myGridPrint);
                              myGridPrint.dataProvider = dataSource;
                              myGridPrint.width = flexPrint1.pageWidth;
                              myGridPrint.height = flexPrint1.pageHeight;
                              myGridPrint.variableRowHeight = true;//
      // try to adjust/create columns from the defaults
      //
                              var oColumnDef:Object;
                              var dgc:DataGridColumn;
                              var aColumnsNew:Array = [];
                              for (var j:int = 0; j < 4; j++)
                              {
                                  dgc = new DataGridColumn();
                                  switch (j)
                                  {
                                      case 0: dgc.width = 75; break;        // description
                                      case 1: dgc.width = 20; break;        // notenum
                                      case 2: dgc.width = 275; break;        // notes
                                      case 3: dgc.width = 100; break;        // date
                                      case 4: dgc.width = 100; break;        // agent
                                      default:dgc.width = 20;
                                  }
                                  dgc.wordWrap = true;
                                  aColumnsNew.push(dgc);
                              }
                              myGridPrint.columns = aColumnsNew;
      //
      // add the columns to the grid then print
      //
                              flexPrint1.addObject(myGridPrint);
                              while (myGridPrint.validNextPage)
                              {
      //
      // loop through the grid for all pages
      //
                                  myGridPrint.nextPage();
                                  flexPrint1.addObject(myGridPrint);
                              }
                              flexPrint1.send();
                              FlexGlobals.topLevelApplication.removeElement(myGridPrint);
                          }
                      }
                      else
                      {
                          Alert.show("Nothing to print","Warning");
                      }
                  }
      
      

       

      This doesn't work.  The advancedatagrid doesn't seem to accept the columns.

       

      Help!