How to print DataGridView in .NET (2.0 and later)? (VB, C#, C++ for .NET Windows Forms)

The following article provides a programmatic approach to printing data within a .NET WinForms DataGridView.

Sometimes all you need to do is print a simple report you are already showing on a monitor screen. Learn how to do this using DataGridViewPrint class in the .NET Framework (2.0 and later). You can do this easily in .NET without overheat of a third-party vendor's reporting package.

The sample code for this article is available at https://www.skaterpro.net/DataGridViewColumnsTrial.zip
You will also need learn more about the DataGridViewColumns .NET assembly and download its trial version that is available for download from https://www.rustemsoft.com/DataGridViewColumns.htm

DataGridViewColumns .NET assembly
More about DataGridView Columns
Download DataGridViewColumns.dll
Order DataGridViewColumns.dll



Basics

To print data within the DataGridView object that is settled down onto your .NET Windows Form, a custom code using the DataGridViewPrint class is used.
The service class is intended to help you to create a print output based on your DataGridView content. The class has been included into DataGridViewColumns .NET assembly since it is built for .NET WinForms DataGridView control and very helpful for your .NET DataGridView design. It will help you to implement an application with a print preview and a print support.
The DataGridViewPrint class is used by the Print dialog and Print Preview dialog on your .NET form to draw the DataGridView object content that should be printed.


Syntax

DataGridViewPrint(PrintDocument1, DataGridView1, bBlackWhite)

PrintDocument1 - System.Drawing.Printing.PrintDocument reusable object that sends output to a printer
DataGridView1 - System.Windows.Forms.DataGridView object that content you are going to print
bBlackWhite - boolean parameter that defines if you like to use "Black and White" printing mode or you would like to send to printer the real coloring that your DataGridView control has currently on your form.


How the .NET DataGridView Printing Works?

Printing DataGridView content is provided directly by the application logic in the .NET Framework. You add a PrintDocument object to the project and handle the PrintPage event which is called every time a new page is to be printed. An object of the DataGridViewPrint class that renders the printed page using GDI+ graphics functions in the PrintPage event handler is used for a .NET PrintDocument object.
A print job is initiated by the PrintDocument's Print method. This starts the print job and raises one or more events. When the print job begins, a BeginPrint event occurs, followed by the PrintPage event for each page, followed by the EndPage event when the job is done. If the print job contains multiple pages, one PrintPage event will be raised for each page in the job making the PrintPage event to execute multiple times. The PrintPage event is the main event involved in printing documents. To send content to the printer you must handle this event and provide code to render the content in the PrintPage event handler.



In the above example when the Print button is clicked the custom frmPrint print dialog form will be open. The frmPrint form contains several dialog checkboxes and buttons. By using this custom Print setting form you can manage your DataGridView printing output. You may define if the DataGridView?s header title will be printed or not by assigning DataGridViewPrint class object?s PrintTitle variable. Another Title property helps you to define the caption text on your DataGridView print output. By using the Print setting form you can perform the following actions:
1. Call Print dialog and send DataGridView print output directly to a printer device. The Print dialog box lets you select options for a particular print job. For example, you can specify the printer to use, the range of pages to print, and the number of copies.
2. Call Print Preview dialog. From this dialog, you can preview on the screen how each printed page of your .NET DataGridView will appear, and select page layout, orientation, and the range of pages to print.
3. Call Page Setup dialog. The Page Setup dialog box lets you set the following attributes of the printed .NET DataGridView page:
-The paper type (envelope, legal, letter, and so on)
-The paper source (manual feed, tractor feed, sheet feeder, and so on)
-The page orientation (portrait or landscape)
-The width of the page margins


Example

The following example creates a DataGridViewPrint object and prints the DataGridView.

VB .NET

Private PrintGrid As DataGridViewPrint
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
Dim fpr As New frmPrint()
With fpr
.Title = DataGridView1.CaptionText
.ShowDialog()
If .Result > 0 Then
PrintGrid = New DataGridViewPrint(PrintDocument1, DataGridView1, .bBlackWhite)
PrintGrid.PrintTitle = .bTitle
PrintGrid.Title = .Title
Select Case .Result
Case 1 ' Print
' The Print method prints the DataGridView without using a print dialog.
' Use a PrintDialog when you want to offer the user the ability to choose print settings.
If PrintDialog1.ShowDialog() = DialogResult.OK Then PrintDocument1.Print()
Case 2 ' Page Setup
PageSetupDialog1.ShowDialog()
Case 3 ' Preview
PrintPreviewDialog1.Icon = fpr.Icon
PrintPreviewDialog1.ShowDialog()
End Select
End If
End With
End Sub

' Specify the output to print by handling the PrintPage event
' and by using the Graphics included in the PrintPageEventArgs.
Private Sub printDocument1_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
' Print method of DataGridViewPrint class starts the custom DataGridView's printing process.
e.HasMorePages = PrintGrid.Print(e.Graphics)
End Sub

C#

private DataGridViewPrint PrintGrid;
private void btnPrint_Click(object sender, System.EventArgs e)
{
frmPrint fpr = new frmPrint();
fpr.Title = DataGridView1.CaptionText;
fpr.ShowDialog();
if (fpr.Result > 0)
{
PrintGrid = new DataGridViewPrint(printDocument1, DataGridView1, fpr.bBlackWhite);
PrintGrid.PrintTitle = fpr.bTitle;
PrintGrid.Title = fpr.Title;
if (fpr.Result == 1) // Print
{
if (printDialog1.ShowDialog() == DialogResult.OK)
{
// The Print method prints the DataGridView without using a print dialog.
// Use a PrintDialog when you want to offer the user the ability to choose print settings.
printDocument1.Print();
}
}
else if (fpr.Result == 2) // Page setup
{
pageSetupDialog1.ShowDialog();
}
else if (fpr.Result == 3) // Preview
{
printPreviewDialog1.Icon = fpr.Icon;
printPreviewDialog1.ShowDialog();
}
}
}

// Specify the output to print by handling the PrintPage event
// and by using the Graphics included in the PrintPageEventArgs.
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
// Print method of DataGridViewPrint class starts the custom DataGridView's printing process.
e.HasMorePages = PrintGrid.Print(e.Graphics);
}


Summary

That is about it for this sample. We have added the .NET DataGridView printing support to an application using DataGridViewPrint class, a process that involved vastly less effort than would have been required to code the printing mechanism by hand. If you would like to try out this sample application you will need the sample code for this article, the trial version of DataGridViewPrint and the DataGridViewColumns library. The download links for all of these are listed at the top of this article.
Further information about DataGridViewPrint and other controls by Rustemsoft LLC can be found at https://www.rustemsoft.com





Copyright © 2001-2017 Rustemsoft LLC All Rights Reserved