打印DataGridView时重复相同的页面(VB 2010)

时间:2013-02-11 20:59:26

标签: vb.net printing datagridview

我正在开发一个应用程序来显示学生的工作打印历史记录。我可以将数据拉入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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

 Dim mRow As Integer = 0
 Dim newpage As Boolean = True

e.HasMorePages = False

mRow newpage ,必须在private sub

之外声明

完成并停止计算结尾的页面