1

我正在开发一个应用程序来显示学生的工作打印历史。我能够将数据拉入 DataGridView 并毫无问题地显示它,但是当我去打印数据时,它会提供第一页的复制,直到它出错。单页报告工作正常,所以我认为这必须相当简单。这是处理打印的代码部分:

Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    With dgvPrintHistory
        Dim fmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
        Dim newpage As Boolean = True
        Dim mRow As Integer = 0
        Dim prFont As New Font("Verdana", 22, GraphicsUnit.Point)
        Dim siFont As New Font("Verdana", 9, GraphicsUnit.Point)
        Dim hdrFont As New Font("Verdana", 10, FontStyle.Bold)
        Dim y As Single = e.MarginBounds.Top
        Dim strStudentInfo As String

        If PrintDocument1.DefaultPageSettings.Landscape Then
            strStudentInfo = "Pages Printed (Semester): " & lblPrintPages.Text & vbTab & "Semester Balance: " & lblBalance.Text & vbTab & "Reporting Dates: " & lblDates.Text
            fmt.LineAlignment = StringAlignment.Center
            fmt.Trimming = StringTrimming.EllipsisCharacter
            e.Graphics.DrawImage(picICC.Image, 130, 10)
            e.Graphics.DrawString(vbTab & "        Student Printing Report: " & lblUserName.Text, prFont, Brushes.Black, 60, 40)
            e.Graphics.DrawString(strStudentInfo, siFont, Brushes.Black, 110, 80)
            PrintPreviewDialog1.Document = PrintDocument1
            PrintDocument1.DefaultPageSettings.Margins.Left = 100
            PrintDocument1.DefaultPageSettings.Margins.Right = 100
            PrintDocument1.DefaultPageSettings.Margins.Top = 50
            PrintDocument1.DefaultPageSettings.Margins.Bottom = 50
            y = 120
        Else
            strStudentInfo = "Pages Printed (Semester): " & lblPrintPages.Text & vbTab & "Semester Balance: " & lblBalance.Text & vbTab & "Reporting Dates: " & lblDates.Text
            fmt.LineAlignment = StringAlignment.Center
            fmt.Trimming = StringTrimming.EllipsisCharacter
            e.Graphics.DrawImage(picICC.Image, 90, 10)
            e.Graphics.DrawString(vbTab & "        Student Printing Report: " & lblUserName.Text, prFont, Brushes.Black, 5, 40)
            e.Graphics.DrawString(strStudentInfo, siFont, Brushes.Black, 55, 80)
            PrintPreviewDialog1.Document = PrintDocument1
            PrintDocument1.DefaultPageSettings.Margins.Left = 50
            PrintDocument1.DefaultPageSettings.Margins.Right = 50
            PrintDocument1.DefaultPageSettings.Margins.Top = 100
            PrintDocument1.DefaultPageSettings.Margins.Bottom = 100
            y = 100
        End If
        Do While mRow < .RowCount
            Dim row As DataGridViewRow = .Rows(mRow)
            Dim x As Single = e.MarginBounds.Left
            Dim h As Single = 0

            For Each cell As DataGridViewCell In row.Cells
                Dim rc As RectangleF = New RectangleF(x, y, cell.Size.Width, cell.Size.Height)
                e.Graphics.DrawRectangle(Pens.Black, rc.Left, rc.Top, rc.Width, rc.Height)
                If newpage Then
                    e.Graphics.DrawString(dgvPrintHistory.Columns(cell.ColumnIndex).HeaderText, .Font, Brushes.Black, rc, fmt)
                Else
                    e.Graphics.DrawString(dgvPrintHistory.Rows(cell.RowIndex).Cells(cell.ColumnIndex).FormattedValue.ToString(), .Font, Brushes.Black, rc, fmt)
                End If
                x += rc.Width
                h = Math.Max(h, rc.Height)
            Next
            newpage = False
            y += h
            mRow += 1
            If y + h > e.MarginBounds.Bottom Then
                e.HasMorePages = True
                mRow -= 1
                newpage = True
                Exit Sub
            End If
        Loop
        mRow = 0
    End With
End Sub
4

2 回答 2

1

您需要e.HasMorePages = False在最后一页设置。您还需要设置一个模块级别的页面计数器变量来跟踪您所在的页面。PrintPage 例程只打印一个页面。变量的值范围mRow应该是模块级页面计数器变量的函数。

于 2013-02-12T03:53:27.857 回答
1
 Dim mRow As Integer = 0
 Dim newpage As Boolean = True

e.HasMorePages = False

mRow newpage并且必须在私有子外部声明,

完成了,它最后停止计算页数

于 2013-02-21T11:25:45.330 回答