【问题标题】:Exporting DataGridView to PDF Error将 DataGridView 导出为 PDF 错误
【发布时间】:2017-05-21 06:23:18
【问题描述】:

我在这里看到的代码有问题:How to print datagridview table with its header in vb.net?

我在没有打印机的情况下进行测试,只是将其保存为 .pdf 文件,但它不会导出 DataGridView。谁能帮我?可能是什么问题?

如您所见,它在打印预览中可见:

保存为 pdf 后(因为我没有可用的打印机进行测试):

我的代码:

 Private mRow As Integer = 0
    Private newpage As Boolean = True
    Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    Dim rect As New Rectangle(12, 9, CInt(PrintDocument1.DefaultPageSettings.PrintableArea.Width), lblCurriculumName.Height)
    Dim sf As New StringFormat
    sf.Alignment = StringAlignment.Center
    sf.LineAlignment = StringAlignment.Center
    e.Graphics.DrawString(lblCurriculumName.Text, lblCurriculumName.Font, Brushes.Black, rect, sf)
    sf.Alignment = StringAlignment.Center

    Dim rect1 As New Rectangle(97, 60, CInt(PrintDocument1.DefaultPageSettings.PrintableArea.Width), txtFullName.Height)
    Dim sf1 As New StringFormat
    sf1.Alignment = StringAlignment.Near
    sf1.LineAlignment = StringAlignment.Near
    e.Graphics.DrawString(txtFullName.Text, txtFullName.Font, Brushes.Black, rect1, sf1)
    sf1.Alignment = StringAlignment.Center

    ' sets it to show '...' for long text
    Dim fmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
    fmt.LineAlignment = StringAlignment.Center
    fmt.Trimming = StringTrimming.EllipsisCharacter
    Dim y As Int32 = e.MarginBounds.Top
    Dim rc As Rectangle
    Dim x As Int32
    Dim h As Int32 = 0
    Dim row As DataGridViewRow

    ' print the header text for a new page
    '   use a grey bg just like the control
    If newpage Then
        row = DataGridView1.Rows(mRow)
        x = e.MarginBounds.Left
        For Each cell As DataGridViewCell In row.Cells
            ' since we are printing the control's view,
            ' skip invidible columns
            If cell.Visible Then
                rc = New Rectangle(x, y, cell.Size.Width, cell.Size.Height)

                e.Graphics.FillRectangle(Brushes.LightGray, rc)
                e.Graphics.DrawRectangle(Pens.Black, rc)

                ' reused in the data pront - should be a function
                Select Case DataGridView1.Columns(cell.ColumnIndex).DefaultCellStyle.Alignment
                    Case DataGridViewContentAlignment.BottomRight,
                         DataGridViewContentAlignment.MiddleRight
                        fmt.Alignment = StringAlignment.Far
                        rc.Offset(-1, 0)
                    Case DataGridViewContentAlignment.BottomCenter,
                        DataGridViewContentAlignment.MiddleCenter
                        fmt.Alignment = StringAlignment.Center
                    Case Else
                        fmt.Alignment = StringAlignment.Near
                        rc.Offset(2, 0)
                End Select

                e.Graphics.DrawString(DataGridView1.Columns(cell.ColumnIndex).HeaderText,
                                           DataGridView1.Font, Brushes.Black, rc, fmt)
                x += rc.Width
                h = Math.Max(h, rc.Height)
            End If
        Next
        y += h

    End If
    newpage = False

    ' now print the data for each row
    Dim thisNDX As Int32
    For thisNDX = mRow To DataGridView1.RowCount - 1
        ' no need to try to print the new row
        If DataGridView1.Rows(thisNDX).IsNewRow Then Exit For

        row = DataGridView1.Rows(thisNDX)
        x = e.MarginBounds.Left
        h = 0

        ' reset X for data
        x = e.MarginBounds.Left

        ' print the data
        For Each cell As DataGridViewCell In row.Cells
            If cell.Visible Then
                rc = New Rectangle(x, y, cell.Size.Width, cell.Size.Height)

                ' pick up any RowPrePaint rule
                If Convert.ToString(row.Cells(2).Value.ToString) = "NG" Then
                    Using br As New SolidBrush(Color.MistyRose)
                        e.Graphics.FillRectangle(br, rc)
                    End Using
                End If

                e.Graphics.DrawRectangle(Pens.Black, rc)

                Select Case DataGridView1.Columns(cell.ColumnIndex).DefaultCellStyle.Alignment
                    Case DataGridViewContentAlignment.BottomRight,
                         DataGridViewContentAlignment.MiddleRight
                        fmt.Alignment = StringAlignment.Far
                        rc.Offset(-1, 0)
                    Case DataGridViewContentAlignment.BottomCenter,
                        DataGridViewContentAlignment.MiddleCenter
                        fmt.Alignment = StringAlignment.Center
                    Case Else
                        fmt.Alignment = StringAlignment.Near
                        rc.Offset(2, 0)
                End Select

                e.Graphics.DrawString(cell.FormattedValue.ToString(),
                                     DataGridView1.Font, Brushes.Black, rc, fmt)

                x += rc.Width
                h = Math.Max(h, rc.Height)
            End If


        Next
        y += h
        ' next row to print
        mRow = thisNDX + 1

        If y + h > e.MarginBounds.Bottom Then
            e.HasMorePages = True
            mRow -= 1   'causes last row to rePrint on next page
            newpage = True
            Return
        End If
    Next

End Sub

Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click

    'need to start fresh each time
    mRow = 0
    newpage = True

    PrintPreviewDialog1.Document = PrintDocument1
    'optionally reset the first page shown
    PrintPreviewDialog1.PrintPreviewControl.StartPage = 0
    PrintPreviewDialog1.WindowState = FormWindowState.Maximized
    PrintPreviewDialog1.ShowDialog()

End Sub

【问题讨论】:

    标签: .net vb.net printing datagridview


    【解决方案1】:

    文档作为预览打印后,如果您决定打印,则必须再次打印该文档。这意味着PrintDocument1_PrintPage 中的所有代码必须再次运行,这次将输出发送到打印机。

    但是,在预览结束时,mRow 将是最大 DGV 行数,newpage 为 false,因此看起来没有任何内容可打印。解决方案是在BeginPrint事件中初始化那些:

    Private Sub PrintDocument1_BeginPrint(sender As Object, 
                   e As PrintEventArgs) Handles PrintDocument1.BeginPrint
        mRow = 0
        newpage = True
        PrintPreviewDialog1.PrintPreviewControl.StartPage = 0
        PrintPreviewDialog1.PrintPreviewControl.Zoom = 1.0
    End Sub
    

    现在,当打印机再次开始打印时,它们被设置为处理整个文档。该代码还会重置要显示的第一页和初始缩放。

    【讨论】:

    • 哇@Plutonix 代码现在真的有效,我明白了。你真的在我的论文中帮助了我很多,呵呵..我应该承认你。谢谢先生。
    猜你喜欢
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 2018-09-12
    • 1970-01-01
    • 1970-01-01
    • 2018-12-29
    • 2016-03-05
    • 1970-01-01
    相关资源
    最近更新 更多