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 :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 :
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
Informations :