This article demonstrates printing of a simple
datagridview using the
Graphics class in C# .NET 2.0.
Using the Code
The best part of .NET is the organization of classes like the
PrintDocument class which is derived from the Printing Class and Printing class is in turn derived from
System.Drawing class which has methods for drawing Shapes, Bitmaps and Text so the same methods can be used by the
To print a
datagridview, we require a table with rows and columns. A table is nothing but a collection of rectangles as mentioned above.
PrintDocument class supports drawing of shapes, so we can easily draw a rectangle given the parameters. The rectangle method is overloaded. The one in which we are more interested is:
DrawRectangle(Pens p,int x,int y, int width,int height)
y are the co-ordinates on the print page in pixel units, the other two parameters specify the
height of the rectangle.
We can specify the co-ordinates, say draw the first rectangle at location 20,20 but how do we get the exact
height of the rectangle? Not to worry! We can get the
width parameter from:
and height parameter from:
At this point, we have gathered all the required information to draw a rectangle but when and where to Draw?
PrintDocument supports an event called
PrintPage which has 2 parameters; the one which we are interested is of type
PrintPageEventArgs which supports drawing of rectangle.
Here's a small snippet:
private void printDocument1_PrintPage
(object sender, System.Drawing.Printing.PrintPageEventArgs e)
e.Graphics.DrawRectangle(Pens.Black, 100, 100,
e.Graphics.FillRectangle(Brushes.LightGray, new Rectangle
(100, 100, dataGridView1.Columns.Width, dataGridView1.Rows.Height));
dataGridView1.Font, Brushes.Black, new RectangleF(100, 100,
dataGridView1.Columns.Width, dataGridView1.Rows.Height), str);
Now we are able to draw or say replicate the first column of the
datagridview on the page at location 100,100.
Now let's draw the second column which adjoins the first column horizontally. The position of y co-ordinate remains the same as columns span horizontally but the x position of the second column will change. It will now become
100 + dataGridView1.Columns.Width.
So repeat the above code in the snippet by just changing the
x position of the
DrawRectangleF method respectively. Here's the first one:
e.Graphics.DrawRectangle(Pens.Black, 100 + dataGridView1.Columns.Width, 100,
To keep things simple, we will restrict our
datagridview to only 2 columns. Now quickly let us draw the rows one by one by using the same trick as discussed above.
Store the origins(
y) in variables, say
width= x + dataGridView1.Columns.Width;
Before coding, there are few more important points to be discussed.
- We can get the number of Rows in a
datagridview view by using the
- We need to run a loop to draw all the rows as is in the
datagridveiw one by one.
- We need to check if your table height is exceeding the page height. If it exceeds, then the printing should continue on a new page, before directly printing the row on the new page first it should draw the column headers and continue drawing the row.
- Every time we print a new row, we need to increase the table height (nothing but
y co-ordinate) by
rowheight and let the
x co-ordinate be at the same position, in our case it's
- When we are printing the cells of a row, we only need to increase the x co-ordinate by
colwidth each time and let the y co-ordinate(
height) remain stationary, i.e.
width += colwidth;
Here's the code snippet
while (i < dataGridView1.Rows.Count)
colwidth= dataGridView1.Columns.Width ;
if (height > e.MarginBounds.Height)
height = x;
width = y;
e.HasMorePages = true;
height += rowheight;
e.Graphics.DrawRectangle(Pens.Black, x, height,colwidth,rowheight);
dataGridView1.Font, Brushes.Black, new RectangleF(x, height, colwidth,rowheight), str);
e.Graphics.DrawRectangle(Pens.Black, y+ colwidth, height, colwidth, rowheight);
dataGridView1.Font, Brushes.Black, new RectangleF(x+ colwidth,
height, width, rowheight), str);
width += colwidth;
Behind the print button, type:
Note: Declare the variable
static or a class level variable:
- 6th November, 2009: Initial post
My first love is C ,was alomost about to marry java(loved it too) but life took a different turn and finally ended by marrying C#.Now got adjusted to .NET and happy with it.
but occasionally code in C