title: vb.net how to export pdf from datagridview
body: <p>i manage to link the mssql get the data and generate to pdf. But now i have a problem in i only can get my sql database header row, others data row cannot be show. any suggestion to solve this problem. Sorry for my english ,hope can uderstand my question.</p>
  
  <p>this is my code</p>
  
  <pre><code>Public Class Form1
  Dim sCommand As SqlCommand
  Dim sAdapter As SqlDataAdapter
  Dim sBuilder As SqlCommandBuilder
  Dim sDs As DataSet
  Dim sTable As DataTable
  
  Private Sub load_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles load_btn.Click
      Dim connectionString As String = "Server= SHQN080-0116; Database= HKT ;Integrated Security=SSPI;"
      Dim sql As String = "SELECT * FROM SalArea"
      Dim connection As New SqlConnection(connectionString)
      connection.Open()
      sCommand = New SqlCommand(sql, connection)
      sAdapter = New SqlDataAdapter(sCommand)
      sBuilder = New SqlCommandBuilder(sAdapter)
      sDs = New DataSet()
      sAdapter.Fill(sDs, "SalArea")
      sTable = sDs.Tables("SalArea")
      connection.Close()
      DataGridView1.DataSource = sDs.Tables("SalArea")
      DataGridView1.ReadOnly = True
      save_btn.Enabled = False
      DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
  
  End Sub
  Private Sub export_btn_Click(sender As System.Object, e As System.EventArgs) Handles export_btn.Click
      Dim pdfTable As New PdfPTable(DataGridView1.ColumnCount)
      pdfTable.DefaultCell.Padding = 3
      pdfTable.WidthPercentage = 30
      pdfTable.HorizontalAlignment = Element.ALIGN_LEFT
      pdfTable.DefaultCell.BorderWidth = 1
  
      'Adding Header row
      For Each column As DataGridViewColumn In DataGridView1.Columns
          Dim cell As New PdfPCell(New Phrase(column.HeaderText))
          cell.BackgroundColor = New iTextSharp.text.Color(240, 240, 240)
          pdfTable.AddCell(cell)
      Next
  
      'Adding DataRow
      For Each row As DataGridViewRow In DataGridView1.Rows
          For Each cell As DataGridViewCell In row.Cells
              pdfTable.AddCell(cell.Value.ToString())
          Next
      Next
  
      'Exporting to PDF
      Dim folderPath As String = "C:\PDFs\"
      If Not Directory.Exists(folderPath) Then
          Directory.CreateDirectory(folderPath)
      End If
      Using stream As New FileStream(folderPath &amp; "DataGridViewExport.pdf", FileMode.Create)
          Dim pdfDoc As New Document(PageSize.A2, 10.0F, 10.0F, 10.0F, 0.0F)
          PdfWriter.GetInstance(pdfDoc, stream)
          pdfDoc.Open()
          pdfDoc.Add(pdfTable)
          pdfDoc.Close()
          stream.Close()
      End Using
  End Sub
  </code></pre>
  
  <p>End Class</p>