|
 |
|
How to print datagrid in .NET? (VB.NET, C#, C++ for .NET Windows Forms)
|
 The following article provides a programmatic approach to printing data within a .NET WinForms Datagrid.
|
 Basics
|
To print data within the Datagrid object that is settled down onto your .NET Windows Form, a custom code using the DataGridPrint class is used.
The service class is intended to help you to create a print output based on your datagrid content. The class has been included into DataGridColumns .NET assembly since it is built for .NET WinForms datagrid control and very helpful for your .NET datagrid design. It will help you to implement an application with a print preview and a print support.
The DataGridPrint class is used by the Print dialog and Print Preview dialog on your .NET form to draw the DataGrid object content that should be printed.
 Syntax
DataGridPrint(PrintDocument1, DataGrid1, bBlackWhite)
PrintDocument1 - System.Drawing.Printing.PrintDocument reusable object that sends output to a printer
DataGrid1 - System.Windows.Forms.DataGrid 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 datagrid control has currently on your form.
 How the .NET DataGrid Printing Works?
Printing datagrid 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 DataGridPrint 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 datagrid printing output. You may define if the datagrids header title will be printed or not by assigning DataGridPrint class objects PrintTitle variable. Another Title property helps you to define the caption text on your datagrid print output. By using the Print setting form you can perform the following actions:
1. Call Print dialog and send datagrid 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 datagrid 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 datagrid 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 DataGridPrint object and prints the Datagrid.
|
 Summary
|
That is about it for this sample. We have added the .NET datagrid printing support to an application using DataGridPrint 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 DataGridPrint and the DataGridColumns library. The download links for all of these are listed at the top of this article.
Further information about DataGridPrint and other controls by Rustemsoft can be found at http://www.Rustemsoft.com
|
|