1
這裏是我的代碼打印:
Private Sub btnPrint_Click(sender As System.Object, e As System.EventArgs) Handles btnPrint.Click
PrintInvoiceDoc.DefaultPageSettings.Landscape = True
PrintPreviewDialog.ShowDialog()
End Sub
Private Sub PrintInvoiceDoc_PrintPage(sender As System.Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintInvoiceDoc.PrintPage
Dim CenterAlign As New StringFormat
CenterAlign.Alignment = StringAlignment.Center
Dim RightAlign As New StringFormat
CenterAlign.Alignment = StringAlignment.Far
e.Graphics.DrawString("School Name", New Font("Arial", 16, FontStyle.Bold), Brushes.Black, New Point(25, 25))
e.Graphics.DrawString("Report", New Font("Arial", 16, FontStyle.Bold), Brushes.Black, New Point(1000, 25), RightAlign)
Dim mRow As Integer = 0
Dim newpage As Boolean = True
With DataGridView1
Dim fmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
fmt.LineAlignment = StringAlignment.Center
fmt.Trimming = StringTrimming.EllipsisCharacter
Dim y As Single = e.MarginBounds.Top
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(DataGridView1.Columns(cell.ColumnIndex).HeaderText, .Font, Brushes.Black, rc, fmt)
Else
e.Graphics.DrawString(DataGridView1.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:我錯過了第一行[你可以看到]
問題#2:當行數足夠多時一個頁面不足以存儲所有這些頁面,新頁面將在無限循環中創建並且同一頁面會重複出現,因此不會打印所有數據。
注:我幾乎不知道如何在PrintDocument
中使用DataGridView
,但歡迎提供任何建議/提示!
也請告訴我,任何交替的方式這樣做......即的ReportViewer *步驟一步一步,如果你可以* –
看看這個鏈接。最初的代碼是用於舊的'DataGrid'控件,但後來增加了一個'DataGridView'版本。 http://www.vbforums.com/showthread.php?356115-DataGridPrinter-A-class-to-print-data-grid-in-a-nicely-formatted-way – jmcilhinney
[將DataTable傳遞給ReportViewer Vb](http: //stackoverflow.com/questions/34511856/pass-datatable-to-reportviewer-vb) –