HomeVb.NetVideo

VB.NET How to Export DataGridView to PDF Using DataTable MySQL Database

VB.NET for beginners : How to Export data from DataGridView into PDF File Using DataTable and connections MySQL Database with ODBC class? DataGridView windows applications tutorial for beginner.

C# CRUD Operations Insert,Update,Delete with MySQL Database
VB.NET Chart Example With Values From MySQL Database + Source Code
C# Tutorial: Insert Data Into DataGridView MySQL Database + Source Code
VB.NET for Beginners - Export Data from DataGridView to PDF Format in VB.NET is easy to do, we will use iTextSharp.dll to create a PDF file and save into our computer with Pdf Format. Before follow this tutorial, you must have a database (MySQL Database). Because in this tutorial we will bind data into datagridview from MySQL database using DataTable, Just read :
How to create MySQL database with xampp PhpMyAdmin in Localhost?
How to Bind data from MySQL Database into DataGridView?

Export Data To PDF vb.net

We will start making project data export into .pdf format, so just open our visual studio applications, i'll using 2015 versions of visual studio, sure you can use more versions of visual studio.

Create Project Export PDF

Create new project and rename it with "VB-Net-Export", and on the form1.vb we will design with simple design look like this display :

Export DataGridView to PDF
After create design on Form1.vb, we will add References (iTextSharp.dll) into our project, iTextSharp.dll is a .net pdf library and we will import iTextSharp.dll namespaces into form1.vb. you can download  iTextSharp.dll here. Download and unzip the .dll file and import into references our project :

Impoer namespaces iTextSharp.dll

After all have done, we will leave the form1.vb for a while, we will create a new connections with MySQL Database using ODBC class, so just create a new module and rename it with "ModuleConnection.Vb".

Source Code Module Connection (ModuleConnection.VB)

Imports System.Data.Odbc ' import namespaces ODBC
Module ModuleConnections
    Public koneksi As OdbcConnection ' declaration our connetcions to public class
    Sub OpenCOnnection()
        Try
            ' create our connection to database using ODBC driver
            koneksi = New OdbcConnection("DSN=k13new;MultipleActiveResultSets=True")
            If koneksi.State = ConnectionState.Closed Then
                'open our connection
                koneksi.Open()
            End If
        Catch ex As Exception
            'if connection is filed
            MsgBox("Connection filed!")
        End Try
    End Sub
End Module

so we done here, just back into Form1.vb and write all of this code.

Source Code Export Data to PDF (Form1.Vb)

Imports System.Data.Odbc ' import namespaces ODBC class
Imports iTextSharp.text ' import namespaces .net pdf library
Imports iTextSharp.text.pdf
Imports System.IO
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        openconnection() ' open our connection
        Dim da As OdbcDataAdapter ' declaration data adapter
        Dim dt As DataTable ' declaration data table
        da = New OdbcDataAdapter("SELECT idsiswa,nama,nis,tempatlahir,alamat FROM biodata", connection)
        dt = New DataTable
        da.Fill(dt)
        DataGridView1.DataSource = dt ' bind data table into datagridview
        DataGridView1.Refresh()
        connection.Close() ' close our connection
        da.Dispose()
        'configuration fo save file dialog
        SaveFileDialog1.FileName = ""
        SaveFileDialog1.Filter = "PDF (*.pdf)|*.pdf"
        TextBox1.Text = "" ' for title
        TextBox2.Text = "" ' for file locations
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        SaveFileDialog1.FileName = ""
        If SaveFileDialog1.ShowDialog = DialogResult.OK Then
            ' declaration textbox2 to save file dialog name
            TextBox2.Text = SaveFileDialog1.FileName
        End If
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        ' you must import itextsharp namespace into our form
        ' download links is available in the descriptions
        Dim Paragraph As New Paragraph ' declaration for new paragraph
        Dim PdfFile As New Document(PageSize.A4, 40, 40, 40, 20) ' set pdf page size
        PdfFile.AddTitle(TextBox1.Text) ' set our pdf title
        Dim Write As PdfWriter = PdfWriter.GetInstance(PdfFile, New FileStream(TextBox2.Text, FileMode.Create))
        PdfFile.Open()

        ' declaration font type
        Dim pTitle As New Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 14, iTextSharp.text.Font.BOLD, BaseColor.BLACK)
        Dim pTable As New Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 12, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)

        ' insert title into pdf file
        Paragraph = New Paragraph(New Chunk(TextBox1.Text, pTitle))
        Paragraph.Alignment = Element.ALIGN_CENTER
        Paragraph.SpacingAfter = 5.0F

        ' set and add page with current settings
        PdfFile.Add(Paragraph)

        ' create data into table
        Dim PdfTable As New PdfPTable(DataGridView1.Columns.Count)
        ' setting width of table
        PdfTable.TotalWidth = 500.0F
        PdfTable.LockedWidth = True

        Dim widths(0 To DataGridView1.Columns.Count - 1) As Single
        For i As Integer = 0 To DataGridView1.Columns.Count - 1
            widths(i) = 1.0F
        Next

        PdfTable.SetWidths(widths)
        PdfTable.HorizontalAlignment = 0
        PdfTable.SpacingBefore = 5.0F

        ' declaration pdf cells
        Dim pdfcell As PdfPCell = New PdfPCell

        ' create pdf header
        For i As Integer = 0 To DataGridView1.Columns.Count - 1

            pdfcell = New PdfPCell(New Phrase(New Chunk(DataGridView1.Columns(i).HeaderText, pTable)))
            ' alignment header table
            pdfcell.HorizontalAlignment = PdfPCell.ALIGN_LEFT
            ' add cells into pdf table
            PdfTable.AddCell(pdfcell)
        Next

        ' add data into pdf table
        For i As Integer = 0 To DataGridView1.Rows.Count - 2

            For j As Integer = 0 To DataGridView1.Columns.Count - 1
                pdfcell = New PdfPCell(New Phrase(DataGridView1(j, i).Value.ToString(), pTable))
                PdfTable.HorizontalAlignment = PdfPCell.ALIGN_LEFT
                PdfTable.AddCell(pdfcell)
            Next
        Next
        ' add pdf table into pdf document
        PdfFile.Add(PdfTable)
        PdfFile.Close() ' close all sessions

        ' show message if hasben exported
        MessageBox.Show("PDF format success exported !", "Informations", MessageBoxButtons.OK, MessageBoxIcon.Information)

    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Me.Close()
    End Sub

    Private Sub LinkLabel1_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        Process.Start("www.hc-kr.com")
    End Sub
End Class

we have done here. Just try your simple applications using debugging mode or press "F5" key on your screen. If you are still confused by tutorials above, please see and following this video tutorial :

Video tutorial How to Export DataGridView to PDF




Feel free to code it up and send us a pull request.

Hi everyone, let's me know how much this lesson can help your work. Please Subscribe and Follow Our Social Media 'kodeajaib[dot]com' to get Latest tutorials and will be send to your email everyday for free!, Just hit a comment if you have confused. Nice to meet you and Happy coding :) all ^^



Follow by E-Mail


COMMENTS

DISQUS
Name

ADO.NET,3,Ajax,6,Android,9,AngularJS,4,ASP.NET,4,Blogger Tutorials,7,Bootstrap,7,C++,1,Codeigniter,2,Cplusplus,6,Crystal Report,6,CSharp,25,Ebook Java,2,FlyExam,1,FSharp,3,Game Development,2,Java,35,JDBC,2,Laravel,89,Lumen,2,MariaDB,2,Ms Access,3,MySQL,31,ODBC,6,OleDB,1,PHP,14,PHP Framework,6,PHP MYSQLI,9,PHP OOP,5,Python,8,Python 3,4,SQL Server,4,SQLite,4,Uncategorized,5,Vb 6,2,Vb.Net,89,Video,48,Vue Js,4,WPF,2,Yii,3,
ltr
item
KODE AJAIB: VB.NET How to Export DataGridView to PDF Using DataTable MySQL Database
VB.NET How to Export DataGridView to PDF Using DataTable MySQL Database
VB.NET for beginners : How to Export data from DataGridView into PDF File Using DataTable and connections MySQL Database with ODBC class? DataGridView windows applications tutorial for beginner.
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjMYB7lGiynwWJchA2uuOQGGWrJlgJ9HIZMwPYxujgsxZoLx56wicM8yaTeMiDuGa4C3FSABfb8oOEDHDw81TtxBUEeD_yRF3E51ZDQVt0TS5S4LfpJL6kOP81twwLfXB2vdC9kt1b3F38/s320/export-data-to-pdf.jpg
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjMYB7lGiynwWJchA2uuOQGGWrJlgJ9HIZMwPYxujgsxZoLx56wicM8yaTeMiDuGa4C3FSABfb8oOEDHDw81TtxBUEeD_yRF3E51ZDQVt0TS5S4LfpJL6kOP81twwLfXB2vdC9kt1b3F38/s72-c/export-data-to-pdf.jpg
KODE AJAIB
https://scqq.blogspot.com/2016/06/vbnet-how-to-export-datagridview-to-pdf.html
https://scqq.blogspot.com/
https://scqq.blogspot.com/
https://scqq.blogspot.com/2016/06/vbnet-how-to-export-datagridview-to-pdf.html
true
3214704946184383982
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy