Click here to Skip to main content
15,610,839 members
Articles / Programming Languages / C#
Posted 18 Jun 2009

Tagged as


47 bookmarked

DataGridView Print/Print Preview Solution - Part II

Rate me:
Please Sign up or sign in to vote.
5.00/5 (15 votes)
18 Jun 2009CPOL4 min read
This article is the second one of two articles in which I want to show a solution for the Print Preview of the DataGridView object. One of the goals of my solution is to print the DataGridView keeping its styles automatically.


This article is the second of two articles that I wrote about Print Preview for the DataGridView object. In the first one, I've written about how to use a new class, the GridPrintDocument, with the standard PrintPreviewDialog.

Now I'm going to show you how to create a personal preview dialog and how to use it.

The solution attached to the article contains the full library for the Print Preview (GridPrintPreviewLib) and a sample application with data (TestApp). Test data are in an XML file, so no settings are necessary to use the sample.


The basic class for the Print and Print Preview is the PrintDocument that I've extended to create the GridPrintDocument. The GridPrintDocument is the real engine for the printing. There are also the GLPrintPreviewControl and frmPreviewDialog classes. They is the user interface for the preview and to handle the user interaction for page settings. To get the image for the preview, I've used the GetPreviewPageInfo() method of the GridPrintDocument. Please note that in the GridPrintDocument‘s PrintPreview, a PreviewPrintController is assigned to the PrintController of the GridPrintDocument. See the following code:

private void PrintPreview(bool flagOnlyMeasure)
    PreviewPrintController preview = new PreviewPrintController();
    preview.UseAntiAlias = true;
    this.PrintController = preview;
    m_FlagOnlyMeasure = flagOnlyMeasure;
    m_FlagOnlyMeasure = false;

In the GLPrintPreviewControl, you can see the code below to generate the images for the preview.

public void GeneratePreview()
    Cursor oldCursor = this.Cursor;
    this.Cursor = Cursors.WaitCursor;
    PreviewPageInfo[] p = m_PrintDocument.GetPreviewPageInfo();
    previewPages = p;
    this.Cursor = oldCursor;

I've “replaced” the default Print method in GridPrintDocument of course. This is necessary to force preview and fit elaboration. Also, it means that if you want to print the result to a real printer, you need to generate the preview (it’s not necessary to show it) and then print the current result by using the base print method as below:

m_PrintDocument.PrintController = null;
m_PrintDocument.PrinterSettings.PrinterName = printerName;

Using the Code

The library can be used inside every project. It’s only necessary to add references and nothing more. However, you need to have a DataGridView in your project, of course. Below, you can see the basic code to show the Print Preview of a DataGridView using the frmPreviewDialog. The dialog will manage the interaction with the user.

// dataGridView1 is the DataGridView to print
GridPrintDocument doc = new GridPrintDocument
			(this.dataGridView1, this.dataGridView1.Font, true);
doc.DocumentName = "Preview Test";
doc.StepForFit = 0.02f;
frmPreviewDialog frmDialog = new frmPreviewDialog();
frmDialog.ShowUnit = PrinterUnit.TenthsOfAMillimeter;
frmDialog.printDocument = doc;

In order to use the frmPreviewDialog, you need to create the object and set ShowUnit and printDocument. printDocument is the GridPrintDocument to preview and ShowUnit is to set how you want to show the margins (millimeters, inches or display unit). In GridPrintDocument, there’s a new attribute named StepForFit. This value is used to reduce the fit factor when the fit action is not perfect. Let me explain. To fit the grid in n pages, I calc a reduce factor in this way:

float scaleX = (m_PageSize.Width * m_FitToHorizontalPages) / m_MaxWidth;
float scaleY = (m_PageSize.Height * m_FitToVerticalPages) / m_MaxHeight;

Unfortunately this expression doesn't consider that a column cannot split into different pages, so it’s possible that when applying these reduce factors (scaleX and scaleY), the result is out of the requested range. To resolve this problem, I check if the output is inside the range: if it’s not, I reduce the factors by a percentage (StepForFit), while 0.02F means 2%, and then reapply the factor for generating a new preview. See the following piece of code:

// Print for fit
bool flag = false;
    flag = false;
    m_ScaleX = scaleX;
    m_ScaleY = scaleY;
    if (m_XPageCount > m_FitToHorizontalPages)
        flag = true;
        scaleX *= (1f - m_StepForFit);
    if (m_YPageCount > m_FitToVerticalPages)
        flag = true;
        scaleY *= (1f - m_StepForFit);
} while (flag);

Points of Interest

To show the current page in the preview window, I've done a set of operations in graphics and then used a PictureBox to show the result. This is necessary because if you show the image got from PreviewPageInfo methods, its size is greater than the real physical size of the paper. Then, it’s necessary to draw the image in a bitmap with the correct size and then show it in a PictureBox using the StrechImage mode. A scale factor for the zoom is also applied. So, please note the following action in order to show the image correctly:

  1. Create a bitmap using the physical size and pixel format of the page
  2. Get a graphics from the bitmap
  3. Clear the bitmap by drawing a white rectangle
  4. Draw the image of the current page
  5. Set size and size mode to the PictureBox
  6. Set the created bitmap as the image of the PictureBox
// Show current page in preview
private void ShowCurrentPage()
    if (m_PreviewPages == null)
    // Dispose previous image
    if (pbPreviewImage.Image != null)
        pbPreviewImage.Image = null;
    // Create the bitmap
    Bitmap bitmap = new Bitmap(m_PreviewPages[m_Currentpage].PhysicalSize.Width, 
    using (Graphics gr = Graphics.FromImage(bitmap))
        // Empty white rectangle
        gr.FillRectangle(Brushes.White, 0, 0, bitmap.Width, bitmap.Height);
        // Draw preview image
        gr.DrawImage(m_PreviewPages[m_Currentpage].Image, 0, 0, 
				bitmap.Width, bitmap.Height);
    double scale = CalcZoomFactorForFit(bitmap);
    double maxWidth = bitmap.Width;
    double maxHeight = bitmap.Height;
    maxWidth *= scale;
    maxHeight *= scale;
    pbPreviewImage.Width = (int)maxWidth;
    pbPreviewImage.Height = (int)maxHeight;
    pbPreviewImage.Left = (pPreview.Width - pbPreviewImage.Width) / 2;
    pbPreviewImage.Top = (pPreview.Height - pbPreviewImage.Height) / 2;
    pbPreviewImage.SizeMode = PictureBoxSizeMode.StretchImage;
    pbPreviewImage.Image = bitmap;

The most complicated elaborations are performed inside the GridPrintDocument‘s OnPrintPage. One of these is to check if there're more horizontal or vertical pages. To do that, I've used two different flags that I'll check at the end of the methods. Please see below:

sizeF = onMeasureCell(e.Graphics, cell, s, m_Font);
// Check Y : use sizeF.Height instead of cellSize.Height 
// if you want to measure string and not cell
//if (y + sizeF.Height > scaleMarginRB.Y)
if (y + cellSize.Height > scaleMarginRB.Y)
         hasMoreYPage = true;
// Check X : use sizeF.Width instead of cellSize.Width 
// if you want to measure string and not cell
//if (x + sizeF.Width > scaleMarginRB.X)
if (x + cellSize.Width > scaleMarginRB.X)
         hasMoreXPage = true;
         maxCol = m_ColIndex;
// Set flag HasMorePages if there're more pages
e.HasMorePages = hasMoreXPage || hasMoreYPage;

One more elaboration is to draw the next horizontal page correctly. In this case, I've used 2 variables to know from which row and column I need to start in the next page. m_RowIndex is the current index of row, so I go back replacing its values with the value of m_PrevPageRowIndex. m_PrevPageColIndex is the last column in the previous page, so I set it with the maxCol (max column in this page). When I start the new page, I'll use the m_RowIndex and m_PrevPageColIndex values.

// Start to check new page
while (m_RowIndex <= m_SelArea.EndRow)
         // Start row for first col
         m_ColIndex = m_PrevPageColIndex;
         while (m_ColIndex <= m_SelArea.EndCol)
// If more X page restore start row as this page and col from next col
if (hasMoreXPage)
         m_RowIndex = m_PrevPageRowIndex;
         m_PrevPageColIndex = maxCol;


  • 18th June, 2009: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Team Leader Mediatech Solutions
Italy Italy
I’m an IT Project Manager for an Italian Betting Company and over the last 2 years I acquired experience in Betting area.
I have developed code in different object oriented languages (C#, C++, Java) for more than 10 years using a set of technology such as .Net, J2EE, multithreading, etc…

Comments and Discussions

Questionhow about a bound datasource. Pin
mmansf1-Oct-17 17:58
mmansf1-Oct-17 17:58 
AnswerRe: how about a bound datasource. Pin
mmansf2-Oct-17 16:58
mmansf2-Oct-17 16:58 
Questioncolumn distances Pin
Member 1121142413-Oct-16 1:30
Member 1121142413-Oct-16 1:30 
GeneralPrint Title on page Pin
some1s6911-Jun-10 10:04
some1s6911-Jun-10 10:04 
GeneralRe: Print Title on page Pin
Member 1047265526-Feb-14 11:09
Member 1047265526-Feb-14 11:09 
GeneralRe: Print Title on page Pin
Alessandro Lentini27-Feb-14 2:14
professionalAlessandro Lentini27-Feb-14 2:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.