Category / Section
How to print a grid with the WinForm screen in WinForms GridControl?
Handle the PrintPage event
You can handle the PrintPage event during printing or print preview, and the page is drawn by the Graphic object of PrintPageEventArgs as follows.
void Document_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//set the image
Bitmap bt = new Bitmap(this.Width, this.Height);
//set the renderering to the specified bitmap
this.DrawToBitmap(bt, new Rectangle(0, 0, this.Width, this.Height));
//draw the image in the given location.
e.Graphics.DrawImage(bt, new Point(0, 0));
}
private void button1_Click_1(object sender, EventArgs e)
{
PrintPreviewDialog ppd = new PrintPreviewDialog();
ppd.Document = new System.Drawing.Printing.PrintDocument();
ppd.Document.PrintPage += Document_PrintPage;
//show the PrintPreviewDialog
ppd.ShowDialog();
}Private Sub Document_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
'set the image
Dim bt As New Bitmap(Me.Width, Me.Height)
'set the renderering to the specified bitmap
Me.DrawToBitmap(bt, New Rectangle(0, 0, Me.Width, Me.Height))
'draw the image in the given location.
e.Graphics.DrawImage(bt, New Point(0, 0))
End Sub
Private Sub button1_Click_1(ByVal sender As Object, ByVal e As EventArgs)
Dim ppd As New PrintPreviewDialog()
ppd.Document = New System.Drawing.Printing.PrintDocument()
AddHandler ppd.Document.PrintPage, AddressOf Document_PrintPage
'show the PrintPreviewDialog
ppd.ShowDialog()
End SubAfter applying the properties, the Grid is displayed as follows.
Figure 1: Print Preview of the Windows Form Grid
Samples:
Reference Link: Printing