Solved

Help with exporting data from DataGrid View to PDF format

Posted on 2013-11-04
4
503 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

Title # Comments Views Activity
vb.net class 3 21
Help with preventing selection from a combobox 11 34
SSRS 2016 Rendering HTML tables 3 31
VB.net SQLCommandBuilder Insert New Records From DataGridView 3 25
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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

829 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