Articles in this section
Category / Section

How to print the WinForms DataGrid (SfDataGrid) control?

1 min read

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

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