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: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

RSS Feeds