Save only visible rows of datagridview to pdf

  • Thread starter Thread starter KeesBlunder
  • Start date Start date
K

KeesBlunder

Guest
found this to save to pdf.

Works but it is saving everything .

But i want to save only the visible rows to pdf and not Id and the other that i hide in the datagridview.

'Creating iTextSharp Table from the DataTable data
Dim pdfTable As New PdfPTable(DataGridView2.ColumnCount)
pdfTable.DefaultCell.Padding = 3
pdfTable.WidthPercentage = 110
pdfTable.HorizontalAlignment = Element.ALIGN_LEFT
pdfTable.DefaultCell.BorderWidth = 1
'Adding Header row
For Each column As DataGridViewColumn In DataGridView2.Columns
Dim cell As New PdfPCell(New Phrase(column.HeaderText))
cell.BackgroundColor = New iTextSharp.text.BaseColor(240, 240, 240)
pdfTable.AddCell(cell)
Next
'Adding DataRow
For Each row As DataGridViewRow In DataGridView2.Rows
For Each cell As DataGridViewCell In row.Cells
If cell.Value IsNot Nothing AndAlso cell.Value.ToString <> "" Then
pdfTable.AddCell(cell.Value.ToString())
Else
pdfTable.AddCell("")
End If
Next
Next
'Exporting to PDF
Dim folderPath As String = "C:\PDF\"
If Not Directory.Exists(folderPath) Then
Directory.CreateDirectory(folderPath)
End If
Using stream As New FileStream(folderPath & "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

MsgBox("Done")

Continue reading...
 
Back
Top