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: May 6, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT While you likely have very good reasons for remaining on WinXP after end of support -- an estimated 20-30% of worldwide devices still are -- the bottom line is your security risk is now significant. In the absence of security patches, attackers will certainly turn their attention to this new opportunity. Join Lumension Vice President Paul Zimski in this one-hour webcast to discuss risk and, more importantly, 5 pragmatic risk mitigation techniques …

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds