sfdatagrid print preview and printing

hi,

I investigate your documentation of printing and  c# sample (I convert to vb.net),

but I can't solve.

can you share vb.net sample or codes 

thanks for your help

 


5 Replies

SS Sampathnarayanan Sankaralingam Syncfusion Team January 24, 2022 02:07 PM UTC

Hi Serdar,


You can print the content of sfdatagrid using the PrintDailog support. Please find the below code snippet for reference.


Private Sub PrintButton_Click(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


Sample Link : https://www.syncfusion.com/downloads/support/forum/172189/ze/PrintPreview_DataGrid_Vb1595597878


Regard,

Sampath Narayanan.S



SE Serdar January 24, 2022 02:33 PM UTC

Hi  Sampath ,

It's work, but my grid had a few unvisible column.

how can i print only visible columns

thanks for your help




VS Vijayarasan Sivanandham Syncfusion Team January 26, 2022 08:45 AM UTC

Hi Serdar,

Your requirement to print only visible columns in SfDataGrid can be achieved by customize the PdfExportingOptions.

By default, all the columns in SfDataGrid will be exported to PDF. If you want to exclude hidden columns while exporting to PDF, you can use ExcludeColumns field in PdfExportingOptions. Please refer the below code snippet,

 
Private Sub PrintButton_Click(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() 
 
                Dim options As New PdfExportingOptions() 
 
                Dim hiddenColumns As List(Of String) = New List(Of String)() 
                'While Exporting Hidden column Stop by Add the MappingName Of hidden column In ExcludeColumns In PdfExportingOptions  
                For Each column In sfDataGrid1.Columns 
                                If Not column.Visible Then hiddenColumns.Add(column.MappingName) 
                Next 
                options.ExcludeColumns = hiddenColumns 
 
                pdfDoc = sfDataGrid1.ExportToPdf(options) 
                '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 

Sample Link: https://www.syncfusion.com/downloads/support/forum/172189/ze/SfDataGridDemo753953412

For more information related to ExcludeColumns options in PdfExportingOptions, please refer the user guide documentation, 


Regards, 
Vijayarasan S 



SE Serdar replied to Vijayarasan Sivanandham January 27, 2022 10:05 PM UTC

hi  Vijayarasan ,

It's work

thanks for help



VS Vijayarasan Sivanandham Syncfusion Team January 28, 2022 05:48 AM UTC

Hi Serdar,

We are glad to know that the reported problem has been resolved at your end. Please let us know if you have any further queries on this. We are happy to help you😊.

Regards,
Vijayarasan S


Loader.
Up arrow icon