PrintPreviewDialog In Windows Forms DataGridView C# VB.NET

PrintPreviewDialog Control With DataGridView In Winforms Or Windows Forms Application Using C# And Vb.net

Printpreviewdialog in winforms windows forms datagridview C# vb.Net


In this post i'm explaining how to use printpreviewdialog with C# and vb.net to preview before printing datagridview in windows forms application.

Drag and place one printPreviewDialog control on the page, open it's property window and assign document to be previewed (printdocument1) to it's document property or assign it in code behind.

you can go to link mentioned above to know how to create printdocument.


C# CODE
private void btnPrint_Click(object sender, EventArgs e)
        {
            //Assign printPreviewDialog properties
            pvDialog.Document = printDocument1;
            pvDialog.PrintPreviewControl.Zoom = 1;
            pvDialog.ShowDialog();
        }

        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)
 pvDialog.Document = printDocument1
 pvDialog.PrintPreviewControl.Zoom = 1
 pvDialog.ShowDialog()
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

Download Sample Code



If you like this post than join us or share

0 comments:

Find More Articles