Paginate and Print MSFlexGrid Content the Easy Way


This article was contributed by Jordi Duatis.

Environment: VC6

MSFlexGrid is a useful control to present data in a Grid, but what happens when this content has to be printed? Here I present a class, PrintGrid, that paginates and prints the grid content. It uses a second Grid control and must be additionally inserted in your CFormView dialog resource or in your dialog.

The instructions are as follows:

  • Add a second MSFlexGrid control to your form or dialog.
  • Change the member in your view or dialog class from CMSFlexGrid to CPrintGrid* m_pxPrintGrid.
  • Change the DDX command: DDX_Control(pDX, IDC_MSFLEXGRID2, *m_pxPrintGrid).
  • Instantiate the control in your constructor: m_pxPrintGrid = new CPrintGrid().
  • Override the operation OnPreparePrinting and OnPrint as follows:
  • BOOL MyFormView::OnPreparePrinting(CPrintInfo* pInfo)
    {
      // Calculate pages for all grid data
      CPrintDialog xPrintDlg(FALSE);
      CDC xDC;
      xPrintDlg.GetDefaults();
      xDC.Attach(xPrintDlg.GetPrinterDC());
      m_pxPrintGrid->Paginate(&xDC, m_pxGrid);
      xDC.Detach();
    
      // Update the number of pages according to DC size
      pInfo->m_nNumPreviewPages = m_pxPrintGrid->GetNumPages();
      pInfo->SetMinPage(1);
      pInfo->SetMaxPage(m_pxPrintGrid->GetNumPages());
    
      // Display printer setup dialog and initialize CPrintInfo
      if (!DoPreparePrinting(pInfo))
      {
        return FALSE;
      }
      // call parent
      return CFormView::OnPreparePrinting(pInfo);
    }
    
    void MyFormView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
    {
      CString sTitle;
      sTitle = "My App title";
      m_pxPrintGrid->PrintPage(pDC, pInfo->m_nCurPage, m_pxGrid,
                               sTitle);
      // Call parent
      CFormView::OnPrint(pDC, pInfo);
    }
    

Enjoy it! I hope it will be useful.

Downloads

Download Class Code: PrintGrid.zip - 4 Kb



Comments

  • Having problem using it

    Posted by souvik_mss on 05/26/2006 07:22am

    Add a second MSFlexGrid control to your form or dialog. Is not clear to me. Why do I need to have second instance of the control placed? The one which I want to print and second one placed should both be of type CPrintGrid? Done the modification related to header file inlcusion and placed a second control and done as stated. But at the time of printing the app hangs and does print nothing. A usage posting may help to understand it better.

    Reply
  • It does work! You just need to play with it a little

    Posted by ltur on 01/10/2006 08:33am

    To get rid of some of the errors mentioned in the previous comments: a) select no precompiled headers in Project Options b) make sure all necessary header files are inlcuded in "stdafx.h" and else where needed c) overwrite the handling of date in header printing function, as it is looking for some resource string that is not included with the downloaded files (you can add your own easily to resource.h) With all these minor stuff, it is still a good functionality, thanks to the author!

    Reply
  • Cool !

    Posted by Legacy on 02/26/2004 12:00am

    Originally posted by: Werner Abt

    Thanx for this code!
    That�s just what i needed! It work�s great except for the print preview. With a Font of 12pt in the Grid, you can�t read the content of the grid. Do you have any suggestion to solve this?

    Thanx a lot

    Reply
  • How to print the MSFlexGrid from dialog

    Posted by Legacy on 12/08/2003 12:00am

    Originally posted by: Harry h

    CFormView::OnPreparePrinting(pInfo) &
    CFormView::OnPrint(pDC, pInfo)
    are protected methodes that can't be called from CDialog
    My Grid control is in CDialog.

    Is there any other option ??

    Best Regards!!

    Harry H


    Reply
  • please give me a sample?thanks

    Posted by Legacy on 09/19/2003 12:00am

    Originally posted by: Robin Lee

    please give me a sample?thanks

    Reply
  • Paginate and Print MSFlexGrid Content the Easy Way

    Posted by Legacy on 06/08/2003 12:00am

    Originally posted by: BJ


    Print MSFlexGrid code is not working

    printgrid.cpp(281) : fatal error C1010: unexpected end of file while looking for precompiled header directive
    Error executing cl.exe.

    code stops at
    GridPage::GridPage(int iStartRow, int iStartCol, int iEndRow, int iEndCol)
    {
    m_iStartRow = iStartRow;
    m_iStartCol = iStartCol;
    m_iEndRow = iEndRow;
    m_iEndCol = iEndCol;
    }

    Reply
  • Help!!!

    Posted by Legacy on 04/23/2003 12:00am

    Originally posted by: Chen Sining

    Hi,

    In VC5, it does not work well. How can I do?

    Thanks for any help!

    Chen Sining

    Reply
  • CMSFlexGrid

    Posted by Legacy on 02/11/2003 12:00am

    Originally posted by: Saj

    Code not working ,please post sample code

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • On-demand Event Event Date: October 23, 2014 Despite the current "virtualize everything" mentality, there are advantages to utilizing physical hardware for certain tasks. This is especially true for backups. In many cases, it is clearly in an organization's best interest to make use of physical, purpose-built backup appliances rather than relying on virtual backup software (VBA - Virtual Backup Appliances). Join us for this webcast to learn why physical appliances are preferable to virtual backup appliances, …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds