Paginate and Print MSFlexGrid Content the Easy Way
Posted
by Jordi Duatis
on February 7th, 2003
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:22amAdd 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.
ReplyIt does work! You just need to play with it a little
Posted by ltur on 01/10/2006 08:33amTo 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!
ReplyCool !
Posted by Legacy on 02/26/2004 12:00amOriginally 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
ReplyHow to print the MSFlexGrid from dialog
Posted by Legacy on 12/08/2003 12:00amOriginally 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
Replyplease give me a sample?thanks
Posted by Legacy on 09/19/2003 12:00amOriginally posted by: Robin Lee
please give me a sample?thanks
ReplyPaginate and Print MSFlexGrid Content the Easy Way
Posted by Legacy on 06/08/2003 12:00amOriginally 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
ReplyGridPage::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:00amOriginally posted by: Chen Sining
Hi,
In VC5, it does not work well. How can I do?
Thanks for any help!
Chen Sining
ReplyCMSFlexGrid
Posted by Legacy on 02/11/2003 12:00amOriginally posted by: Saj
Code not working ,please post sample code
Reply