Articles in this section
Category / Section

How to preview WinForms DataGrid (SfDataGrid) control in print preview dialog?

1 min read

Printing

You can create print preview for the content of WinForms DataGrid using PDF exporting and PrintDialog support. For that, export the SfDataGrid to PDF and load the exported PDF document into the PdfViewerControl. Then, the PdfViewerControl can be used to display the Print Preview window using PrintPreviewDialog.

C#

void PrintPreviewButton_Click(object sender, System.EventArgs e)
{
    PrintPreviewDialog printdialog = new PrintPreviewDialog();
    PdfViewerControl pdfviewer = new PdfViewerControl();
    MemoryStream pdfstream = new MemoryStream();
    var document = sfDataGrid1.ExportToPdf();
    document.Save(pdfstream);
    pdfviewer.Load(pdfstream);
    printdialog.Document = pdfviewer.PrintDocument;
    printdialog.ShowDialog();   
}

VB

Private Sub PrintPreviewButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
 Dim printdialog As New PrintPreviewDialog()
 Dim pdfviewer As New PdfViewerControl()
 Dim pdfstream As New MemoryStream()
 Dim document = sfDataGrid1.ExportToPdf()
 document.Save(pdfstream)
 pdfviewer.Load(pdfstream)
 printdialog.Document = pdfviewer.PrintDocument
 printdialog.ShowDialog()
End Sub

 

Show the print preview dialog of DataGrid

Samples:

C#: PrintPreview_CS

VB: PrintPreview_VB

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