如何打印DataGridView数据?

时间:2016-06-27 23:48:18

标签: vb.net visual-studio-2010 printing datagridview

Screenshot of the Window

这是我打印的代码:

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:当行足够以至于一个页面不足以存储所有页面时,将在无限循环中创建新页面并重复相同的页面,因此不会打印所有数据。

注意:我几乎不知道在DataGridView中使用PrintDocument,但欢迎任何建议/提示!

1 个答案:

答案 0 :(得分: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