首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用DataGridView中的条件批量打印

使用DataGridView中的条件批量打印
EN

Stack Overflow用户
提问于 2020-07-15 16:44:57
回答 1查看 136关注 0票数 0

我正在尝试使用vb.net从DataGridView批量打印数据,每页一行。当前代码只是循环。我想要处理和打印所有行,并只询问一次在哪里保存PDF。顺便说一下,我已经将打印机设置为PDF打印机。输出应为6页。

到目前为止,这是我的代码。

代码语言:javascript
复制
Imports System.Drawing.Printing
Imports System.Text.RegularExpressions

Public Class Form1
    Public currentRow As Integer
    Public totPages As Integer
    Public pageNumber As Integer
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        For i As Integer = 0 To 5
            DataGridView1.Rows.Add("Yes", "someData" & i, "someData" & i, "someData" & i, "someData" & i)
        Next


    End Sub
    Private Sub printButton_Click(sender As Object, e As EventArgs) Handles printButton.Click
        Dim pdS As New PrintDocument()

        pdS.DefaultPageSettings.Landscape = False

        Dim PrintDialog1 As New PrintDialog
        PrintDialog1.Document = pdS
        totPages = 0
        pageNumber = 0
        For Each row As DataGridViewRow In DataGridView1.Rows
            If row.Cells(0).Value = "Yes" Then
                totPages = totPages + 1
            End If
        Next

        AddHandler pdS.PrintPage, AddressOf pds_PrintPage

        If (PrintDialog1.ShowDialog = DialogResult.OK) Then
            For Each row As DataGridViewRow In DataGridView1.Rows
                If row.Cells(0).Value = "Yes" Then
                    currentRow = row.Index
                    pageNumber = pageNumber + 1
                    pdS.Print()
                End If
            Next
        End If

    End Sub

    Private Sub pds_PrintPage(sender As Object, ev As PrintPageEventArgs)

        'Drawing
        Dim inputData As String
        Dim calReg As New Font("Calibri", 8, FontStyle.Regular)
        With DataGridView1
            inputData = .Rows(currentRow).Cells(1).Value & vbCrLf & .Rows(currentRow).Cells(2).Value & vbCrLf &
                .Rows(currentRow).Cells(3).Value & vbCrLf & .Rows(currentRow).Cells(4).Value
        End With

        Dim nextY As Double
        nextY = ev.MarginBounds.Y
        ev.Graphics.DrawString(inputData, calReg, Brushes.Black, ev.MarginBounds.X, nextY)

        If totPages > pageNumber Then
            ev.HasMorePages = True
            Exit Sub
        Else
            ev.HasMorePages = False
        End If

    End Sub
End Class
EN

回答 1

Stack Overflow用户

发布于 2020-07-15 20:19:07

您可以创建目标DataGridViewRow对象的Queue,在PrintPage处理程序中将一行出队以打印它,如果队列有更多行,则将HasMorePages属性设置为True

下面的方法是打印或预览。

代码语言:javascript
复制
Private Sub Print(Optional IsPreview As Boolean = False)
    Using pdoc As New PrintDocument,
            pd As New PrintDialog With {.Document = pdoc},
            pp As New PrintPreviewDialog With {.Document = pdoc}

        pdoc.DefaultPageSettings.Landscape = False

        If Not IsPreview AndAlso pd.ShowDialog <> DialogResult.OK Then Return

        Dim q As New Queue(Of DataGridViewRow)

        DataGridView1.Rows.Cast(Of DataGridViewRow).
            Where(Function(r) Not r.IsNewRow AndAlso r.Cells(0).Value.ToString = "Yes").
            ToList.ForEach(Sub(r) q.Enqueue(r))

        AddHandler pdoc.PrintPage,
            Sub(s, e)
                Using f As New Font("Calibri", 8, FontStyle.Regular)
                    Dim r = q.Dequeue
                    Dim x = e.MarginBounds.X
                    Dim y = e.MarginBounds.Y

                    For Each cell In r.Cells.Cast(Of DataGridViewCell).Skip(1)
                        e.Graphics.DrawString(cell.Value.ToString, f, Brushes.Black, x, y)
                        y += f.Height
                    Next

                    e.HasMorePages = q.Count > 0
                End Using
            End Sub

        If IsPreview Then
            pp.ShowDialog()
        Else
            pdoc.Print()
        End If
    End Using
End Sub

对于给定的示例,这将打印6页。按如下方式调用该方法:

代码语言:javascript
复制
Private Sub printButton_Click(sender As Object, e As EventArgs) Handles printButton.Click
    Print() 'To print...
    Print(True) 'To preview...
End Sub
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62910963

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档