如何使用e.HasMorePages打印多个页面?

时间:2015-07-31 08:19:28

标签: vb.net printing datagridview

我想从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

1 个答案:

答案 0 :(得分:0)

打印要求您在每个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