Select to view content in your preferred language

Print Multipage Datagrid

906
3
01-13-2011 04:10 PM
ChrisO_Leary
Emerging Contributor
Hello,

I'm hoping someone can help me out. I've got a couple of datagrid's in my viewer application containing attribute data. I'm trying to add the ability to print these and have found several examples of doing this with the PrintDataGrid component, looping through the pages of data, etc..

My implementation of this does not work even though it is pretty much a line for line copy of some of the examples. The datagrid always seems to indicated that there is additional pages to print, even if there is only a single row. This is resulting in an infinite loop until the flash plugin crashes.

Any help would be appreciated, thanks!

My code.

  public static function printGrid(dg:DataGrid):void
  {
  
   try
   {
    var thePrintJob:FlexPrintJob = new FlexPrintJob();
        
    if (thePrintJob.start())
    {
     
     var scaleType:String = FlexPrintJobScaleType.MATCH_WIDTH;
     var pDataGrid:PrintDataGrid = new PrintDataGrid();
     
     // copy columns and attributes from passed grid
     for (var x:int = 0; x < dg.columns.length; x++)
     {
      var srcCol:DataGridColumn = dg.columns;
      var destCol:DataGridColumn = new DataGridColumn();
      destCol.headerText = srcCol.headerText;
      destCol.dataField = srcCol.dataField;
      
      pDataGrid.columns.push(destCol);
     }
     
     
     pDataGrid.width = thePrintJob.pageWidth;
     pDataGrid.height = thePrintJob.pageHeight;
     pDataGrid.visible = false;
     pDataGrid.includeInLayout = false;
     pDataGrid.dataProvider = dg.dataProvider;
     
     FlexGlobals.topLevelApplication.addElement(pDataGrid);
          
     thePrintJob.addObject(pDataGrid, scaleType);     

     while(pDataGrid.validNextPage) // ** always returns true
     {
      pDataGrid.nextPage();
      
      thePrintJob.addObject(pDataGrid, scaleType);
     }
     
     FlexGlobals.topLevelApplication.removeElement(pDataGrid);

     thePrintJob.send();
    }
    
   }
   catch(e:Error)
   {
    Alert.show(e.errorID + " " + e.message);
   }
   
  } // method



Tags (2)
0 Kudos
3 Replies
DasaPaddock
Esri Regular Contributor
I'd try removing these lines:
pDataGrid.visible = false;
pDataGrid.includeInLayout = false;

And then after this line:
FlexGlobals.topLevelApplication.addElement(pDataGrid);

Add:
pDataGrid.validateNow();
0 Kudos
ChrisO_Leary
Emerging Contributor
Thank you, that was exactly what I needed. I'm sure the example I looked at had those lines about includeInLayout and visible set to false as well, so that's why I put them in.

Can you explain what the effect of what I was doing and what removing them has?
0 Kudos
DasaPaddock
Esri Regular Contributor
Adobe's Flex SDK sample is setting internal parts of the form they're printing to be invisible, but not the form itself. By leaving the pDataGrid visible and then calling validateNow() it's being drawn so that it can then be printed.
0 Kudos