2013-02-11 63 views
1

我正在研究一个应用程序以显示学生的工作打印历史记录。我能够将数据拉入DataGridView中并且毫无问题地显示它,但是当我打印数据时,它会提供第一页的复制,直到它出错。单页报告工作正常,所以我认为这必须是相当简单的事情。这里就是打印处理的代码段:打印时重复同一页面DataGridView(VB 2010)

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 

回答

1

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

+0

我要感谢你们俩 - 你的答案都很有帮助!我使用你的建议(SSS和User2095218)对我的代码进行了一些重构,现在它工作的很好! – TrunksSSJ 2013-02-27 18:50:17

+0

不客气!快乐编程。 – SSS 2013-03-04 00:07:35

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

e.HasMorePages = False

MROW NEWPAGE,并已到外面的私人子声明,

做完这些,它停在末端计数网页