Print DataGridView In WinForms Windows Forms With C# VB.NET

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.

Print Datagridview in winforms windows application using C# and

Printing Datagridview in C# vb.NET

Write below mentioned code in respective events.

private void btnPrint_Click(object sender, EventArgs e)

        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);
        }        }

Private Sub btnPrint_Click(sender As Object, e As EventArgs)
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


Find More Articles