Printing DataGridView With C# VB.NET In Winforms Windows Froms Application.
In this post i'm explaining how to print datagridview in winforms windows application using C# and VB.NET with PrintDocument class.
Drag and place one DataGridView on the form and populate datagridview from database or dataset. I have used northwind database to populate datagridview.
Place one button on the form and name it btnPrint, we will print datagridview in Click event of this button so generate button's click event by double clicking on it.
Drag and place one PrintDocument control on the form from toolbox under printing tab, Double click on it to generate it's PrintPage event.
Write below mentioned code in Click event of print button and PrintPage event of PrintDocument respectively.
c# CODE
VB.NET CODE
Build and run the code.
In this post i'm explaining how to print datagridview in winforms windows application using C# and VB.NET with PrintDocument class.
Drag and place one DataGridView on the form and populate datagridview from database or dataset. I have used northwind database to populate datagridview.
Place one button on the form and name it btnPrint, we will print datagridview in Click event of this button so generate button's click event by double clicking on it.
Drag and place one PrintDocument control on the form from toolbox under printing tab, Double click on it to generate it's PrintPage event.
Write below mentioned code in Click event of print button and PrintPage event of PrintDocument respectively.
c# CODE
private void btnPrint_Click(object sender, EventArgs e)
{
printDocument1.Print();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
PaintEventArgs gridSize = new PaintEventArgs(e.Graphics, new Rectangle(new Point(0, 0), this.Size));
this.InvokePaint(dataGridView1, gridSize);
}VB.NET CODE
Private Sub btnPrint_Click(sender As Object, e As EventArgs) printDocument1.Print() End Sub Private Sub printDocument1_PrintPage(sender As Object, e As System.Drawing.Printing.PrintPageEventArgs) Dim gridSize As New PaintEventArgs(e.Graphics, New Rectangle(New Point(0, 0), Me.Size)) Me.InvokePaint(dataGridView1, gridSize) End Sub
Build and run the code.


