Print DataGridView In WinForms C# VB.NET
Printing DataGridView With C# VB.NET In Winforms Windows Froms Application Using PrintDocument class.
Drag and place DataGridView on the form and populate it from database or dataset. I have used northwind.
Place one button and name it btnPrint, generate it's click event by double clicking on it We will use this event for printing.
Put PrintDocument control from toolbox under printing tab, Double click on it to generate it's PrintPage event.
Write below mentioned code in respective events.
C# CODE
VB.NET CODE
Build and run the code.
Drag and place DataGridView on the form and populate it from database or dataset. I have used northwind.
Place one button and name it btnPrint, generate it's click event by double clicking on it We will use this event for printing.
Put PrintDocument control from toolbox under printing tab, Double click on it to generate it's PrintPage event.
Write below mentioned code in respective events.
C# CODE
01private void btnPrint_Click(object sender, EventArgs e)02 {03 printDocument1.Print();04 }05 06 private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)07 {08 Bitmap dataGridViewImage = new Bitmap(this.dataGridView1.Width, this.dataGridView1.Height);09 dataGridView1.DrawToBitmap(dataGridViewImage, new Rectangle(0, 0, this.dataGridView1.Width, this.dataGridView1.Height));10 e.Graphics.DrawImage(dataGridViewImage, 0, 0);11 } }VB.NET CODE
1Private Sub btnPrint_Click(sender As Object, e As EventArgs)2 printDocument1.Print()3End Sub4 5Private Sub printDocument1_PrintPage(sender As Object, e As System.Drawing.Printing.PrintPageEventArgs)6 Dim dataGridViewImage As New Bitmap(Me.dataGridView1.Width, Me.dataGridView1.Height)7 dataGridView1.DrawToBitmap(dataGridViewImage, New Rectangle(0, 0, Me.dataGridView1.Width, Me.dataGridView1.Height))8 e.Graphics.DrawImage(dataGridViewImage, 0, 0)9End SubBuild and run the code.
.png)
C# GridView printing code
ReplyDelete