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;
      m_pxPrintGrid->Paginate(&xDC, m_pxGrid);
      // Update the number of pages according to DC size
      pInfo->m_nNumPreviewPages = 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,
      // Call parent
      CFormView::OnPrint(pDC, pInfo);

Enjoy it! I hope it will be useful.


Download Class Code: - 4 Kb


  • 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.

  • 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!

  • 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

  • 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

  • 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

  • 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;

  • Help!!!

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

    Originally posted by: Chen Sining


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

    Thanks for any help!

    Chen Sining

  • CMSFlexGrid

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

    Originally posted by: Saj

    Code not working ,please post sample code

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

Top White Papers and Webcasts

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

Most Popular Programming Stories

More for Developers

RSS Feeds