This Example explains how to use PageSetupDialog In Windows Forms Winforms Application With C# And VB.NET to display page setup dialog and Print DataGridView.
DataGridView is populated with Sql database.
To open PageSetupDialog In windows Forms and create bitmap image of data to print,Generate Click event of button by double clicking on it in design view and write following code.
C# CODE
VB.NET CODE
Build and run the application.
DataGridView is populated with Sql database.
To open PageSetupDialog In windows Forms and create bitmap image of data to print,Generate Click event of button by double clicking on it in design view and write following code.
C# CODE
private void btnPrint_Click(object sender, EventArgs e) { PrintDocument printDocument1 = new PrintDocument(); printDocument1.PrintPage += new PrintPageEventHandler(this.printDocument1_PrintPage); PageSetupDialog pageSetup = new PageSetupDialog(); pageSetup.Document = printDocument1; pageSetup.PageSettings = printDocument1.DefaultPageSettings; if (pageSetup.ShowDialog() == DialogResult.OK) { printDocument1.DefaultPageSettings = pageSetup.PageSettings; printDocument1.Print(); } } private void printDocument1_PrintPage(object sender, 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) Dim printDocument1 As New PrintDocument() printDocument1.PrintPage += New PrintPageEventHandler(AddressOf Me.printDocument1_PrintPage) Dim pageSetup As New PageSetupDialog() pageSetup.Document = printDocument1 pageSetup.PageSettings = printDocument1.DefaultPageSettings If pageSetup.ShowDialog() = DialogResult.OK Then printDocument1.DefaultPageSettings = pageSetup.PageSettings printDocument1.Print() End If End Sub Private Sub printDocument1_PrintPage(sender As Object, e As 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 application.
If you like this post than join us or share
0 comments:
Post a Comment