In this article I will explain with an example, how to print DataGridView in Windows Application using C# and VB.Net.
The DataGridView will be printed with the help of PrintPreviewDialog control in Windows Application using C# and VB.Net.
Form Controls
In the below Form, there’s a DataGridView, a Print Button, a PrintPreviewDialog and a PrintDocument controls.
Populating DataGridView
In order to populate the DataGridView, I have created a dynamic DataTable with some sample data.
C#
private void Form1_Load(object sender, EventArgs e)
{
this.BindDataGridView();
}
private void BindDataGridView()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country",typeof(string)) });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
this.dataGridView1.DataSource = dt;
}
VB.Net
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
BindDataGridView()
End Sub
Private Sub BindDataGridView()
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id", GetType(Integer)), New DataColumn("Name", GetType(String)), New DataColumn("Country", GetType(String))})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
Me.dataGridView1.DataSource = dt
End Sub
Printing DataGridView
When the Print Button is clicked, first the DataGridView height is adjusted based on the count of rows so that all rows of the DataGridView are printed.
A Bitmap object is created and the DataGridView is drawn in to the Bitmap object.
Once the DataGridView is drawn, the DataGridView is resized back to its original height.
Finally the PrintPreviewDialog is shown and the Bitmap object is printed.
C#
Bitmap bitmap;
private void btnPrint_Click(object sender, EventArgs e)
{
//Resize DataGridView to full height.
int height = dataGridView1.Height;
dataGridView1.Height = dataGridView1.RowCount * dataGridView1.RowTemplate.Height;
//Create a Bitmap and draw the DataGridView on it.
bitmap = new Bitmap(this.dataGridView1.Width, this.dataGridView1.Height);
dataGridView1.DrawToBitmap(bitmap, new Rectangle(0, 0, this.dataGridView1.Width, this.dataGridView1.Height));
//Resize DataGridView back to original height.
dataGridView1.Height = height;
//Show the Print Preview Dialog.
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.PrintPreviewControl.Zoom = 1;
printPreviewDialog1.ShowDialog();
}
private void PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//Print the contents.
e.Graphics.DrawImage(bitmap, 0, 0);
}
VB.Net
Private bitmap As Bitmap
Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click
'Resize DataGridView to full height.
Dim height As Integer = dataGridView1.Height
dataGridView1.Height = dataGridView1.RowCount * dataGridView1.RowTemplate.Height
'Create a Bitmap and draw the DataGridView on it.
bitmap = New Bitmap(Me.dataGridView1.Width, Me.dataGridView1.Height)
dataGridView1.DrawToBitmap(bitmap, New Rectangle(0, 0, Me.dataGridView1.Width, Me.dataGridView1.Height))
'Resize DataGridView back to original height.
dataGridView1.Height = height
'Show the Print Preview Dialog.
printPreviewDialog1.Document = printDocument1
printPreviewDialog1.PrintPreviewControl.Zoom = 1
printPreviewDialog1.ShowDialog()
End Sub
Private Sub PrintPage(sender As Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles printDocument1.PrintPage
'Print the contents.
e.Graphics.DrawImage(bitmap, 0, 0)
End Sub
Screenshots
Print Preview Dialog displaying the DataGridView
Printed DataGridView
Downloads