Paginate and Print MSFlexGrid Content the Easy Way | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Feb 7, 2003
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.