Blue Theme Orange Theme Green Theme Red Theme
 
MindFusion's Components
Home | Forums | Videos | Photos | Downloads | Blogs | E-Books | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
World Class ASP.NET Hosting - 3 Month Free Hosting, Click Here!
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » Printing » How To Print a Data Grid in C# and .NET

How To Print a Data Grid in C# and .NET

I wrote this article in response to the question, "How the heck do I print out a DataGrid and its contents

Author Rank:
Technologies: Printing,Visual C# .NET
Total downloads : 4646
Total page views :  176458
Rating :
 1.83/5
This article has been rated :  6 times
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
DataGridPrinterMG.zip
 
Become a Sponsor


Related EbooksTop Videos

The DataGrid is a highly versatile component of the .NET architecture and probably one of the most complex components. I wrote this article in response to the question, "How the heck do I print out a DataGrid and its contents".  My first off the cuff suggestion was to capture the form using my screen capture article, but this of course does not solve the problem of printing out the umpteen rows being virtually displayed in the DataGrid.  Then I thought to myself, this should be easy,  I'll just use GDI+ and go through the rows in the DataGrid and print out its contents.  Well the DataGrid is a bit more complex than that because it does not contain the data within itself.  The data is contained within the DataSet.  So the approach I settled on was to capture the color and font properties from the DataGrid for the print out, and the capture the information in the rows from the DataSet.  In order to encapsulate the drawing of the DataGridPrinter to the Printer, I created the DataGridPrinter class shown in Figure 2 below.  This class takes a DataGrid, a PrintDocument, and a DataTable passed to its constructor and utilizes these objects to draw the DataGrid to the printer.

Figure 1. The Print Preview of the Northwind DataGrid

Figure 2. DataGridPrinter Class UML Design (Reverse engineered using WithClass 2000)

The DataGridPrinter is constructed in the constructor of the form so it can be utilized by all of the printing functions (print, print preview, etc.)  Below is the code for constructing the DataGridPrinter:

void SetupGridPrinter()
{
dataGridPrinter1 =
new DataGridPrinter(dataGrid1, printDocument1,
dataSet11.Customers);
}

Once the DataGridPrinter is constructed, you can have it draw the DataGrid to the printer by calling its DrawDataGrid method in the Print Page event handler:

private void printDocument1_PrintPage(object sender,System.Drawing.Pr