【问题标题】:Printing More than One Page with a Loop From DataGridView从 DataGridView 循环打印多页
【发布时间】:2013-06-03 12:21:37
【问题描述】:

我有一个代码正在运行,它从 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)

【问题讨论】:

  • Static i As Integer = 139 .. 那是什么?

标签: vb.net


【解决方案1】:

老实说,我这辈子都没写过一行VB代码,但乍一看打印机制似乎与Java打印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/

【讨论】:

    【解决方案2】:

    希望您理解它可能很复杂,因为您必须计算字体高度、纸张高度等。

    在这种情况下,考虑会有一个固定的设置

    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
    

    【讨论】:

      猜你喜欢
      • 2014-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多