We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to print the WinForms DataGrid (SfDataGrid) control?

Platform: WinForms |
Control: SfDataGrid |
Published Date: September 7, 2018 |
Last Revised Date: January 13, 2020
Tags: printing

Printing

You can print the content of SfDataGrid using PDF exporting and PrintDialog support. For that, export the SfDataGrid to PDF and load the exported PDF document into PdfDocumentView. Then, print the document in PdfDocumentView using the PrintDialog.Document.Print method.

C#

void PrintButton_Click(object sender, System.EventArgs e)
{
    PdfDocumentView pdfDocumentView1 = new PdfDocumentView();
 
    //Create Memory Stream to save pdfdocument.
    MemoryStream pdfstream = new MemoryStream();
 
    PdfDocument pdfDoc = new PdfDocument();
    pdfDoc = sfDataGrid1.ExportToPdf();
 
    //Save the PDF file
    pdfDoc.Save(pdfstream);
 
    //Load the pdfstream to pdfDocumentView
    pdfDocumentView1.Load(pdfstream);
 
    PrintDialog printdialog = new PrintDialog();
    int pageCount = pdfDocumentView1.PageCount;
    printdialog.AllowPrintToFile = true;
    printdialog.AllowSomePages = true;
    printdialog.PrinterSettings.FromPage = 1;
    printdialog.PrinterSettings.ToPage = pageCount;
    printdialog.PrinterSettings.MaximumPage = pageCount;
    printdialog.PrinterSettings.MinimumPage = 1;
 
    if (printdialog.ShowDialog() == DialogResult.OK)
    {
        printdialog.Document = pdfDocumentView1.PrintDocument;
        printdialog.Document.Print();
    }
}

VB

Private Sub Print(ByVal sender As Object, ByVal e As System.EventArgs)
 Dim pdfDocumentView1 As New PdfDocumentView()
 
 'Create Memory Stream to save pdfdocument.
 Dim pdfstream As New MemoryStream()
 
 Dim pdfDoc As New PdfDocument()
 pdfDoc = sfDataGrid1.ExportToPdf()
 
 'Save the PDF file
 pdfDoc.Save(pdfstream)
 
 'Load the pdfstream to pdfDocumentView
 pdfDocumentView1.Load(pdfstream)
 
 Dim printdialog As New PrintDialog()
 Dim pageCount As Integer = pdfDocumentView1.PageCount
 printdialog.AllowPrintToFile = True
 printdialog.AllowSomePages = True
 printdialog.PrinterSettings.FromPage = 1
 printdialog.PrinterSettings.ToPage = pageCount
 printdialog.PrinterSettings.MaximumPage = pageCount
 printdialog.PrinterSettings.MinimumPage = 1
 
 If printdialog.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
  printdialog.Document = pdfDocumentView1.PrintDocument
  printdialog.Document.Print()
 End If
End Sub

 

Show print the grid page of SfDataGrid control

Samples:

C#: Print_CS

VB: Print_VB

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile