【问题标题】:How to print multiple page using e.HasMorePages?如何使用 e.HasMorePages 打印多页?
【发布时间】:2015-07-31 08:19:28
【问题描述】:

我想从 datagridview 打印数据, 但是,当数据超过一页时,它就会丢失。我尝试使用 e.HasMorePages 打印多页。我尝试搜索 3 小时前的示例,但不幸的是它不起作用

现在当我点击打印时,它只打印同一页而不停止。

请帮忙。

Private Sub PrintColorConsumption_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintColorConsumption.PrintPage
    Dim x As Integer = 100
    Dim y As Integer = 25
    Dim header As Boolean = True
    'draw headers
    Dim j As Integer = 0
    Do While (j < Me.DataGridView3.Columns.Count)
        Dim rect As Rectangle = New Rectangle(x, y, Me.DataGridView3.Columns(j).Width, Me.DataGridView3.ColumnHeadersHeight)
        Dim sf As StringFormat = New StringFormat
        sf.LineAlignment = StringAlignment.Center
        sf.Alignment = StringAlignment.Center
        e.Graphics.FillRectangle(Brushes.LightGray, rect)
        e.Graphics.DrawRectangle(Pens.Black, rect)
        If (Not (Me.DataGridView3.Columns(j).HeaderText) Is Nothing) Then
            e.Graphics.DrawString(Me.DataGridView3.Columns(j).HeaderText, SystemFonts.DefaultFont, Brushes.Black, rect, sf)
        End If
        x = (x + rect.Width)
        j = (j + 1)
    Loop
    x = 100
    y = (y + Me.DataGridView3.ColumnHeadersHeight)
    'draw rows
    For Each row As DataGridViewRow In Me.DataGridView3.Rows
        j = 0
        Do While (j < Me.DataGridView3.Columns.Count)
            Dim cell As DataGridViewCell
            cell = row.Cells(j)
            Dim rect As Rectangle = New Rectangle(x, y, cell.Size.Width, cell.Size.Height)
            Dim sf As StringFormat = New StringFormat
            sf.LineAlignment = StringAlignment.Center
            sf.Alignment = StringAlignment.Center
            e.Graphics.DrawRectangle(Pens.Black, rect)
            If (Not (cell.Value) Is Nothing) Then
                e.Graphics.DrawString(cell.Value.ToString, SystemFonts.DefaultFont, Brushes.Black, rect, sf)
            End If
            x = (x + rect.Width)
            j = (j + 1)
        Loop
        x = 100
        y = (y + row.Height)
        '----------------------New page----------------------------
        If (y > e.MarginBounds.Bottom) Then       'Print new page
            e.HasMorePages = True
            y = 20
        End If
        '-----------------------------------------------------------------
    Next
End Sub

【问题讨论】:

    标签: vb.net printing datagridview


    【解决方案1】:

    打印要求您在每个 PrintPage 事件的打印数据中保持位置。声明变量的方式超出范围并在每次引发事件时设置为初始值。

    查看以下未经测试的代码是否有帮助。请注意,在事件处理程序之外声明了一个变量以跟踪当前行。

    Dim whRow As Integer = 0
    Private Sub PrintColorConsumption_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintColorConsumption.PrintPage
        Dim x As Integer = 100
        Dim y As Integer = 25
        Dim header As Boolean = True
        'draw headers
        Dim j As Integer = 0
        Do While (j < Me.DataGridView3.Columns.Count)
            Dim rect As Rectangle = New Rectangle(x, y, Me.DataGridView3.Columns(j).Width, Me.DataGridView3.ColumnHeadersHeight)
            Dim sf As StringFormat = New StringFormat
            sf.LineAlignment = StringAlignment.Center
            sf.Alignment = StringAlignment.Center
            e.Graphics.FillRectangle(Brushes.LightGray, rect)
            e.Graphics.DrawRectangle(Pens.Black, rect)
            If (Not (Me.DataGridView3.Columns(j).HeaderText) Is Nothing) Then
                e.Graphics.DrawString(Me.DataGridView3.Columns(j).HeaderText, SystemFonts.DefaultFont, Brushes.Black, rect, sf)
            End If
            x = (x + rect.Width)
            j = (j + 1)
        Loop
        x = 100
        y = (y + Me.DataGridView3.ColumnHeadersHeight)
        'draw rows
        For whRow = whRow To Me.DataGridView3.RowCount - 1
            Dim drow As DataGridViewRow = Me.DataGridView3.Rows(whRow)
            j = 0
            Do While (j < Me.DataGridView3.Columns.Count)
                Dim cell As DataGridViewCell
                cell = drow.Cells(j)
                Dim rect As Rectangle = New Rectangle(x, y, cell.Size.Width, cell.Size.Height)
                Dim sf As StringFormat = New StringFormat
                sf.LineAlignment = StringAlignment.Center
                sf.Alignment = StringAlignment.Center
                e.Graphics.DrawRectangle(Pens.Black, rect)
                If (Not (cell.Value) Is Nothing) Then
                    e.Graphics.DrawString(cell.Value.ToString, SystemFonts.DefaultFont, Brushes.Black, rect, sf)
                End If
                x = (x + rect.Width)
                j = (j + 1)
            Loop
            x = 100
            y = (y + drow.Height)
            '----------------------New page----------------------------
            If (y > e.MarginBounds.Bottom) Then       'Print new page
                e.HasMorePages = True
                y = 20
            End If
            '-----------------------------------------------------------------
        Next
    End Sub
    

    【讨论】:

    • 感谢您的建议。现在页数是正确的,但第二页只有表头,其余数据仍在第一页并与第一页的数据重叠,因为设置 y=20(e.HasMorePages = True 之后的行)。如果我删除第 y=20 行,重叠数据将消失但不会显示在第二页中还有什么想法吗?
    • 只需在该行之后添加“Exit sub”,现在它可以完美运行。非常感谢@dbasnett。
    • @JenneyMC 没能测试...抱歉。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多