私は学生の仕事の印刷履歴を表示するアプリケーションに取り組んでいます。データを 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