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