Solved

Help with exporting data from DataGrid View to PDF format

Posted on 2013-11-04
4
504 Views
Last Modified: 2013-11-07
Hi,

How do you export data from DataGridView to PDF format using VB.NET?

Thanks,

Victor
0
Comment
Question by:vcharles
  • 2
  • 2
4 Comments
 
LVL 20

Expert Comment

by:ElrondCT
ID: 39623410
Probably the easiest way would be to create output using Crystal Reports, then use its PDF export function. So you wouldn't be directly going from the DataGridView to PDF, but you'd send the DataTable to CR to create the report, then use code something like the following to create the PDF:

Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports System.IO

                crDiskFileDestinationOptions = New DiskFileDestinationOptions
                crExportOptions = rptOut.ExportOptions
                'Export to PDF
                sfdReport.DefaultExt = ".pdf"
                sfdReport.Filter = "Portable Document Format (*.pdf)|*.pdf|All files (*.*)|*.*"
                'set the required report ExportOptions properties
                With crExportOptions
                    .DestinationOptions = crDiskFileDestinationOptions
                    .ExportDestinationType = ExportDestinationType.DiskFile
                    .ExportFormatType = ExportFormatType.PortableDocFormat
                End With
                dlgResult = sfdReport.ShowDialog
                If dlgResult = DialogResult.Cancel Then
                    Exit Sub
                End If
                crDiskFileDestinationOptions.DiskFileName = sfdReport.FileName
               Try
                   ' Export the report
                   rptOut.Export()
                   MsgBox("Report exported successfully.")
               Catch err As Exception
                   MsgBox(err.Message.ToString)
               End Try

Open in new window

0
 

Author Comment

by:vcharles
ID: 39624228
Hi,

Where in the code are you sending data from the DataTable to CR to create the report?

Thanks,

Victor
0
 
LVL 20

Accepted Solution

by:
ElrondCT earned 500 total points
ID: 39624364
I'm not showing the creation of a report. I'm just showing how, after you've created a report, you can send it to PDF.

A good introduction to creating a Crystal Report in VB is at http://www.emoreau.com/Entries/Articles/2006/09/Feeding-Crystal-Reports-from-your-application.aspx (Eric Moreau, the author, is a regular here on EE). A follow-up column, http://www.emoreau.com/Entries/Articles/2006/10/Crystal-Reports--Part-II.aspx, actually has a more succinct way to output the report to PDF.
0
 

Author Closing Comment

by:vcharles
ID: 39631730
Thank You.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question