Articles in this section
Category / Section

How to print a grid with the WinForm screen in WinForms GridControl?

1 min read

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.

 C#

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

VB

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 Sub

After applying the properties, the Grid is displayed as follows.

Print preview of the grid

Figure 1: Print Preview of the Windows Form Grid

Samples:

C#: PrintPreview_winform

VB: PrintPreview_winform

Reference link: https://help.syncfusion.com/windowsforms/grid-control/printing

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied