2016-06-27 70 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