How To Print a DataGrid in C# and .NET

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 printout, 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.

Print DataGrid in C#

Figure 1. The Print Preview of the Northwind DataGrid

Print DataGrid in C#

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:

  1. void SetupGridPrinter()  
  2. {  
  3.     dataGridPrinter1 =new DataGridPrinter(dataGrid1, printDocument1,  
  4.     dataSet11.Customers);  
  5. }  
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:
  1. private void printDocument1_PrintPage(object sender,System.Drawing.Printing.PrintPageEventArgs e)  
  2. {  
  3.     Graphics g = e.Graphics;  
  4.     // Draw a label title for the grid  
  5.     DrawTopLabel(g);  
  6.     // draw the datagrid using the DrawDataGrid method passing the Graphics surface  
  7.     bool more = dataGridPrinter1.DrawDataGrid(g);  
  8.     // if there are more pages, set the flag to cause the form to trigger another print page event  
  9.     if (more == true)  
  10.     {  
  11.         e.HasMorePages =true;  
  12.         dataGridPrinter1.PageNumber++;  
  13.     }  
  14. }  
The PrintPage event is triggered by both the Print method in the PrintDocument and the PrintPreviewDialog's ShowDialog method.  Below is the form's method for printing the DataGrid to the printer:
  1. private void PrintMenu_Click(object sender, System.EventArgs e)  
  2. {  
  3.     // Initialize the datagrid page and row properties  
  4.     dataGridPrinter1.PageNumber = 1;  
  5.     dataGridPrinter1.RowCount = 0;  
  6.     // Show the Print Dialog to set properties and print the document after ok is pressed.  
  7.     if (printDialog1.ShowDialog() == DialogResult.OK)  
  8.     {  
  9.         printDocument1.Print();  
  10.     }  
  11. }  

Now let's take a look at the internals of the DataGridPrinter methods. There are two main methods in the DataGridPrinter class that do all the drawing: DrawHeader and DrawRows. Both these methods extract information from the DataGrid and the DataTable to draw the DataGrid. Below is the method for drawing the rows of the DataGrid:

  1.     public bool DrawRows(Graphics g)  
  2.     {  
  3.         try  
  4.         {  
  5.             int lastRowBottom = TopMargin;  
  6.             // Create an array to save the horizontal positions for drawing horizontal gridlines  
  7.             ArrayList Lines = new ArrayList();  
  8.             // form brushes based on the color properties of the DataGrid  
  9.             // These brushes will be used to draw the grid borders and cells  
  10.             SolidBrush ForeBrush = new SolidBrush(TheDataGrid.ForeColor);  
  11.             SolidBrush BackBrush = new SolidBrush(TheDataGrid.BackColor);  
  12.             SolidBrush AlternatingBackBrush = new SolidBrush  
  13.             TheDataGrid.AlternatingBackColor);  
  14.             Pen TheLinePen = new Pen(TheDataGrid.GridLineColor, 1);  
  15.             // Create a format for the cell so that the string in the cell is cut off at the end of  
  16.             the column width  
  17.             StringFormat cellformat = new StringFormat();  
  18.             cellformat.Trimming = StringTrimming.EllipsisCharacter;  
  19.             cellformat.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.LineLimit;  
  20.             // calculate the column width based on the width of the printed page and the # of  
  21.             columns in the DataTable  
  22.             // Note: Column Widths can be made variable in a future program by playing with the GridColumnStyles of the  
  23.             // DataGrid  
  24.             int columnwidth = PageWidth / TheTable.Columns.Count;  
  25.             // set the initial row count, this will start at 0 for the first page, and be a different  
  26.             value for the 2nd, 3rd, 4th, etc.  
  27.             // pages.  
  28.             int initialRowCount = RowCount;  
  29.             RectangleF RowBounds = new RectangleF(0, 0, 0, 0);  
  30.             // draw the rows of the table   
  31.             for (int i = initialRowCount; i < TheTable.Rows.Count; i++)  
  32.             {  
  33.                 // get the next DataRow in the DataTable  
  34.                 DataRow dr = TheTable.Rows[i];  
  35.                 int startxposition = TheDataGrid.Location.X;  
  36.                 // Calculate the row boundary based on teh RowCount and offsets into the page  
  37.                 RowBounds.X = TheDataGrid.Location.X; RowBounds.Y = TheDataGrid.Location.Y +  
  38.                  TopMargin + ((RowCount - initialRowCount) + 1) * (TheDataGrid.Font.SizeInPoints +  
  39.                  kVerticalCellLeeway);  
  40.                 RowBounds.Height = TheDataGrid.Font.SizeInPoints + kVerticalCellLeeway;  
  41.                 RowBounds.Width = PageWidth;  
  42.                 // save the vertical row positions for drawing grid lines  
  43.                 Lines.Add(RowBounds.Bottom);  
  44.                 // paint rows differently for alternate row colors  
  45.                 if (i % 2 == 0)  
  46.                 {  
  47.                     g.FillRectangle(BackBrush, RowBounds);  
  48.                 }  
  49.                 else  
  50.                 {  
  51.                     g.FillRectangle(AlternatingBackBrush, RowBounds);  
  52.                 }  
  53.                 // Go through each column in the row and draw the information from the  
  54.                 DataRowfor(int j = 0; j < TheTable.Columns.Count; j++)  
  55.                 {  
  56.                 RectangleF cellbounds = new RectangleF(startxposition,  
  57.                 TheDataGrid.Location.Y + TopMargin + ((RowCount - initialRowCount) + 1) *  
  58.                 (TheDataGrid.Font.SizeInPoints + kVerticalCellLeeway),  
  59.                 columnwidth,  
  60.                 TheDataGrid.Font.SizeInPoints + kVerticalCellLeeway);  
  61.                 // draw the data at the next position in the row  
  62.                 if (startxposition + columnwidth <= PageWidth)  
  63.                 {  
  64.                     g.DrawString(dr[j].ToString(), TheDataGrid.Font, ForeBrush, cellbounds, cellformat);  
  65.                     lastRowBottom = (int)cellbounds.Bottom;  
  66.                 }  
  67.                 // increment the column position  
  68.                 startxposition = startxposition + columnwidth;  
  69.             }  
  70.             RowCount++;  
  71.             // when we've reached the bottom of the page, draw the horizontal and vertical grid lines and return true  
  72.         if (RowCount * (TheDataGrid.Font.SizeInPoints + kVerticalCellLeeway) >  
  73.         PageHeight * PageNumber) - (BottomMargin + TopMargin))  
  74.             {  
  75.                 DrawHorizontalLines(g, Lines); DrawVerticalGridLines(g, TheLinePen, columnwidth,  
  76.                  lastRowBottom);  
  77.                 return true;  
  78.             }  
  79.         }  
  80. // when we've reached the end of the table, draw the horizontal and vertical gridlines and return false  
  81.     DrawHorizontalLines(g, Lines);  
  82.     DrawVerticalGridLines(g, TheLinePen, columnwidth, lastRowBottom);  
  83.     return false;  
  84. }  
  85. catch (Exception ex)  
  86. {  
  87. MessageBox.Show(ex.Message.ToString());  
  88. return false;  
  89. }  
The method goes through each row in the DataTable and draws the data. The method uses the properties of the DataGrid to paint each row with the appropriate colors and draw each string with the DataGrid's font. If the method reaches the bottom of the page, it breaks and returns true so that the rest of the DataGrid can be printed on the following page.

Improvements

This class can be greatly improved by utilizing the DataGridColumnStyle class stored in the TableStyles property of the DataGrid. These properties allow you to specify different column width's for certain columns and different text alignments.


Similar Articles