1
votes

I have a datagridview that I'm trying to export in pdf format. I downloaded and implemented a datagridviewtopdf class, but I needed to modify it to dynamically create the necessary columns (the column count will range from 1-12 for months in a year). I also needed to include an extra column to put the row header text for each row in my datagridview. I have the code, which I will include below. I keep getting an OutOfRange exception at the line that I've starred. Any idea how to fix this and create the pdf to resemble my datagridview table?

Edited to also include screenshots of datagridview. Datagridview will have up to 65 rows and up to 12 columns (not including rowheaders or columnheaders

Datagridview example 1 Datagridview example 2, shows how the table dynamically changes between tabs

  Private Function GetDataTable() As System.Data.DataTable
    Dim dataTable As New Data.DataTable("MyDataTable")
    'Create another DataColumn Name
    For column As Integer = 1 To DataGridView1.ColumnCount - 1
        Dim dataColumn_1 As New DataColumn(DataGridView1.Columns(column).HeaderText.ToString(), GetType(String))
        dataTable.Columns.Add(dataColumn_1)
        'Now Add some row to newly created dataTable
        Dim dataRow As DataRow
        For i As Integer = 0 To DataGridView1.Rows.Count - 1
            dataRow = dataTable.NewRow()
            ' Important you have create New row
            dataRow(DataGridView1.Columns(column).HeaderText.ToString()) = DataGridView1.Rows(i).Cells(column).Value.ToString()
            dataTable.Rows.Add(dataRow)
        Next i
    Next column
    dataTable.AcceptChanges()
    Return dataTable
End Function

Private Sub ExportDataToPDFTable()
    Dim paragraph As New Paragraph
    Dim doc As New Document(iTextSharp.text.PageSize.A4, 40, 40, 40, 10)
    Dim wri As PdfWriter = PdfWriter.GetInstance(doc, New FileStream(SaveFileDialog1.FileName + ".pdf", FileMode.Create))
    doc.Open()

    Dim font12BoldRed As New iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 12.0F, iTextSharp.text.Font.UNDERLINE Or iTextSharp.text.Font.BOLDITALIC, BaseColor.RED)
    Dim font12Bold As New iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 12.0F, iTextSharp.text.Font.BOLD, BaseColor.BLACK)
    Dim font12Normal As New iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 12.0F, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)

    Dim p1 As New Phrase
    p1 = New Phrase(New Chunk("PDF From Datagridview Data", font12BoldRed))
    doc.Add(p1)

    'Create instance of the pdf table and set the number of column in that table
    Dim PdfTable As New PdfPTable(DataGridView1.ColumnCount + 1)
    PdfTable.TotalWidth = 490.0F
    'fix the absolute width of the table
    PdfTable.LockedWidth = True
    'relative col widths in proportions - 1,4,1,1 and 1
    'Dim widths As Single() = New Single() {1.0F, 1.0F, 1.0F, 1.0F, 1.0F}
    ' PdfTable.SetWidths(widths)
    PdfTable.HorizontalAlignment = 1 ' 0 --> Left, 1 --> Center, 2 --> Right
    PdfTable.SpacingBefore = 2.0F

    'pdfCell Decleration
    Dim PdfPCell As PdfPCell = Nothing
    'Assigning values to each cell as phrases
    For column As Integer = 0 To (DataGridView1.ColumnCount - 1)
        If column = 0 Then
            For row As Integer = 0 To (DataGridView1.RowCount - 1)
                'Getting out of range exception below for row
                PdfPCell = New PdfPCell(New Phrase(New Chunk(DataGridView1.Rows(row).HeaderCell.ToString, font12Bold)))
                'Add pdfcell in pdftable
                PdfTable.AddCell(PdfPCell)
            Next row

        Else
            PdfPCell = New PdfPCell(New Phrase(New Chunk(DataGridView1.Columns(column).HeaderText, font12Bold)))
            'Alignment of phrase in the pdfcell
            PdfPCell.HorizontalAlignment = PdfPCell.ALIGN_CENTER

            'Add pdfcell in pdftable
            PdfTable.AddCell(PdfPCell)
        End If


    Next column


    Dim dt = GetDataTable()
    If dt IsNot Nothing Then
        'Now add the data from datatable to pdf table
        For rows As Integer = 0 To dt.Rows.Count - 1
            For column As Integer = 0 To dt.Columns.Count - 1
                PdfPCell = New PdfPCell(New Phrase(dt.Rows(rows)(column).ToString(), font12Normal))
                If column = 0 Or column = 1 Then
                    PdfPCell.HorizontalAlignment = PdfPCell.ALIGN_LEFT
                Else
                    PdfPCell.HorizontalAlignment = PdfPCell.ALIGN_RIGHT
                End If
                PdfTable.AddCell(PdfPCell)
            Next
        Next
        'Adding pdftable to the pdfdocument
        doc.Add(PdfTable)
    End If
    doc.Close()
End Sub
1

1 Answers

1
votes

Change:

For column As Integer = 1 To DataGridView1.ColumnCount + 1
For row As Integer = 1 To DataGridView1.RowCount

To:

For column As Integer = 0 To (DataGridView1.ColumnCount - 1)
For row As Integer = 0 To (DataGridView1.RowCount -1)

Remember: It's zero-base indexing.

Example:

Dim dtColumn As DataColumn
Dim dtRow As DataRow

For column As Integer = 0 To (Me.DataGridView1.ColumnCount - 1)
    dtColumn = dt.Columns.Item(Me.DataGridView1.Columns(column).DataPropertyName)
    For row As Integer = 0 To (DataGridView1.RowCount - 1)
        dtRow = DirectCast(Me.DataGridView1.Rows(row).DataBoundItem, DataRowView).Row

        'Replace this:
        'PdfPCell = New PdfPCell(New Phrase(dt.Rows(rows)(column).ToString(), font12Normal))

        'With this:
        PdfPCell = New PdfPCell(New Phrase(row.Item(column).ToString(), font12Normal))

    Next
Next