使用DataGridView循环打印多个页面

时间:2013-06-03 12:21:37

标签: vb.net

我正在使用的代码可以从DataGridView打印销售报告。代码打印效果很好但只能在控件变量I的值超出可打印区域时打印一页。我真的需要你的帮助。以下是代码。

            Dim fntAddress As New Font("Comic Sans MS", 10, FontStyle.Regular)
        Dim fntHeader As New Font("Calibri", 20, FontStyle.Bold)
        Dim fntBodyText As New Font("Calibri", 12, FontStyle.Regular)
        Dim fntHeaderText As New Font("Calibri", 13, FontStyle.Bold)
        Dim strTotalSale = txtTotal.Text

        e.Graphics.DrawString("SFC POINT OF SALE AND INVENTORY MANAGEMENT", fntHeader, Brushes.Black, 100, 0)
        e.Graphics.DrawString("GENERATED SALES REPORT", New Font("Calibri", 18, FontStyle.Bold), Brushes.Black, 250, 30)

        Dim strDateString As String = ""
        If mtbStartDate.Text = "  /  /" Or mtbEndDate.Text = "  /  /" Then
            strDateString = ""
        ElseIf mtbStartDate.Text = mtbEndDate.Text Then
            strDateString = "Report For Date Of : " & mtbStartDate.Text
        ElseIf mtbStartDate.Text <> mtbEndDate.Text Then
            strDateString = "Report For Dates Of : " & mtbStartDate.Text & " - " & mtbEndDate.Text
        End If

        e.Graphics.DrawString(strDateString, New Font("Courier New", 15, FontStyle.Regular), Brushes.Black, 5, 70)

        e.Graphics.DrawRectangle(Pens.Black, New Rectangle(5, 100, 770, 35))
        e.Graphics.DrawString("Item Barcode", fntHeaderText, Brushes.Black, 10, 107)
        e.Graphics.DrawString("Item Name", fntHeaderText, Brushes.Black, 160, 107)
        e.Graphics.DrawString("Quantity", fntHeaderText, Brushes.Black, 360, 107)
        e.Graphics.DrawString("Unit Cost", fntHeaderText, Brushes.Black, 450, 107)
        e.Graphics.DrawString("Sub Total", fntHeaderText, Brushes.Black, 560, 107)
        e.Graphics.DrawString("Date of Sale", fntHeaderText, Brushes.Black, 660, 107)

        Dim RowCount As Integer = dgvSales.Rows.Count - 1
        Static i As Integer = 139
        Dim x1 = 10
        Dim x2 = 700
        Dim y1 = 155
        Dim n As Integer = 0


        While n < RowCount
            e.Graphics.DrawRectangle(Pens.Black, New Rectangle(5, i - 5, 770, 35))
            e.Graphics.DrawString(dgvSales.Rows(n).Cells(0).Value, fntBodyText, Brushes.Black, 16, i)
            e.Graphics.DrawString(dgvSales.Rows(n).Cells(1).Value, fntBodyText, Brushes.Black, 160, i)
            e.Graphics.DrawString(dgvSales.Rows(n).Cells(2).Value, fntBodyText, Brushes.Black, 360, i)
            e.Graphics.DrawString(dgvSales.Rows(n).Cells(3).Value, fntBodyText, Brushes.Black, 450, i)
            e.Graphics.DrawString(dgvSales.Rows(n).Cells(4).Value, fntBodyText, Brushes.Black, 560, i)
            e.Graphics.DrawString(dgvSales.Rows(n).Cells(5).Value, fntBodyText, Brushes.Black, 660, i)
            i = i + 35
            n = n + 1
        End While
        e.HasMorePages = False
        e.Graphics.DrawLine(Pens.Black, 150, 100, 150, i - 5)
        e.Graphics.DrawLine(Pens.Black, 350, 100, 350, i - 5)
        e.Graphics.DrawLine(Pens.Black, 440, 100, 440, i - 5)
        e.Graphics.DrawLine(Pens.Black, 550, 100, 550, i - 5)
        e.Graphics.DrawLine(Pens.Black, 650, 100, 650, i - 5)

        e.Graphics.DrawRectangle(Pens.Black, New Rectangle(340, i + 40, 174, 50))
        e.Graphics.DrawRectangle(Pens.Black, New Rectangle(341, i + 41, 172, 48))
        e.Graphics.DrawRectangle(Pens.Black, New Rectangle(342, i + 42, 170, 46))
        e.Graphics.DrawRectangle(Pens.Black, New Rectangle(343, i + 43, 168, 44))
        e.Graphics.DrawString("Total Sales.", New Font("Times New Roman (Headings CS)", 22, FontStyle.Bold), Brushes.Black, 341, i + 47)

        e.Graphics.DrawRectangle(Pens.Black, New Rectangle(525, i + 40, 250, 50))
        e.Graphics.DrawRectangle(Pens.Black, New Rectangle(526, i + 41, 248, 48))
        e.Graphics.DrawRectangle(Pens.Black, New Rectangle(527, i + 42, 246, 46))
        e.Graphics.DrawRectangle(Pens.Black, New Rectangle(528, i + 43, 244, 44))
        e.Graphics.DrawString(strTotalSale, New Font("Times New Roman (Headings CS)", 23, FontStyle.Bold), Brushes.Black, 538, i + 47)

2 个答案:

答案 0 :(得分:0)

老实说,我的生活中没有编写过一行VB代码,但打印机制一目了然,与Java Printing API非常相似。

您必须使用e.hasMorePages = True标记您正在打印的文档本身有更多页面这一事实。

这是一个工作示例: http://www.dreamincode.net/forums/topic/139447-help-with-hasmorepages-please/

考虑到您必须始终知道您在前一页上停留的地方这一事实。这是另一个更现实的例子:http://www.dreamincode.net/forums/topic/128639-ehasmorepages-and-logic-to-use-it/

答案 1 :(得分:0)

希望你明白它可能很复杂,因为你必须计算fontheight,paperize height等。

在这种情况下,请考虑是否存在固定设置

Dim nMaxLine as Integer = 30 ' ----------> lines count per page
Dim x as Integer
Dim n as Integer 

For x = 0 to datagridview.rows.count - 1
  ' ..
  e.Graphics.DrawString( ... )
  ' ..
  n += 1
  If n > nMaxLine Then
    e.hasMorePages = True
    n = 0
  End If
Next