Printing DataGridView With C# VB.NET In Winforms Windows Froms Application Using PrintDocument class. Drag and place DataGridView on the Windows 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 winform.
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.
Place one button and name it btnPrint, generate it's click event by double clicking on it We will use this event for printing winform.
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
private void btnPrint_Click(object sender, EventArgs e) { printDocument1.Print(); } private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { Bitmap dataGridViewImage = new Bitmap(this.dataGridView1.Width, this.dataGridView1.Height); dataGridView1.DrawToBitmap(dataGridViewImage, new Rectangle(0, 0, this.dataGridView1.Width, this.dataGridView1.Height)); e.Graphics.DrawImage(dataGridViewImage, 0, 0); } }
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 dataGridViewImage As New Bitmap(Me.dataGridView1.Width, Me.dataGridView1.Height) dataGridView1.DrawToBitmap(dataGridViewImage, New Rectangle(0, 0, Me.dataGridView1.Width, Me.dataGridView1.Height)) e.Graphics.DrawImage(dataGridViewImage, 0, 0) End Sub
Build and run the code.
If you like this post than join us or share
0 comments:
Post a Comment