【问题标题】:Repetition of same page when printing DataGridView (VB 2010)打印 DataGridView (VB 2010) 时重复同一页面
【发布时间】:2013-02-11 20:59:26
【问题描述】:

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

【问题讨论】:

    标签: vb.net printing datagridview


    【解决方案1】:

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

    【讨论】:

    • 我要感谢你们两个——你们的回答都很有帮助!我使用您的建议(SSS 和 User2095218)对我的代码进行了一些重组,现在它工作正常!
    【解决方案2】:
     Dim mRow As Integer = 0
     Dim newpage As Boolean = True
    

    e.HasMorePages = False

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-30
      • 2012-12-18
      • 2011-07-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多