2016-06-27 117 views
1

Screenshot of the Window如何打印DataGridView數據?

這裏是我的代碼打印:

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 

所以,打 '打印' 當我得到這個圖像中顯示的結果是:

Print Screenshot

問題1:我錯過了第一行[你可以看到]

問題#2:當行數足夠多時一個頁面不足以存儲所有這些頁面,新頁面將在無限循環中創建並且同一頁面會重複出現,因此不會打印所有數據。

注:我幾乎不知道如何在PrintDocument中使用DataGridView,但歡迎提供任何建議/提示!

+0

也請告訴我,任何交替的方式這樣做......即的ReportViewer *步驟一步一步,如果你可以* –

+0

看看這個鏈接。最初的代碼是用於舊的'DataGrid'控件,但後來增加了一個'DataGridView'版本。 http://www.vbforums.com/showthread.php?356115-DataGridPrinter-A-class-to-print-data-grid-in-a-nicely-formatted-way – jmcilhinney

+0

[將DataTable傳遞給ReportViewer Vb](http: //stackoverflow.com/questions/34511856/pass-datatable-to-reportviewer-vb) –

回答

0

背後標題行烏爾第一排......使烏拉圭回合標題行這樣的新的RectangleF ....

For Each cell As DataGridViewCell In row.Cells 
       Dim rc As RectangleF = New RectangleF(x, y, cell.Size.Width, cell.Size.Height) 
       Dim rc2 As RectangleF = New RectangleF(x, y - 22, cell.Size.Width, cell.Size.Height) 
       If (newpage) Then 
        e.Graphics.FillRectangle(New SolidBrush(Color.LightGray), New RectangleF(x, y - 22, cell.Size.Width, cell.Size.Height)) 
        e.Graphics.DrawRectangle(Pens.Black, rc2.Left, rc2.Top, rc2.Width, rc2.Height) 
        e.Graphics.DrawString(dgpembelian.Columns(cell.ColumnIndex).HeaderText, cellheaderFont, Brushes.Black, rc2, drawFormat) 
       End If 
       e.Graphics.DrawRectangle(Pens.Black, rc.Left, rc.Top, rc.Width, rc.Height) 
       e.Graphics.DrawString(dgpembelian.Rows(cell.RowIndex).Cells(cell.ColumnIndex).FormattedValue.ToString, .Font, Brushes.Black, rc, drawFormat) 
       x += rc.Width 
       h = Math.Max(h, rc.Height) 
      Next