使用DataGridView中的条件进行批量打印



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

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

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

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

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

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页。按如下方式调用方法:

Private Sub printButton_Click(sender As Object, e As EventArgs) Handles printButton.Click
Print() 'To print...
Print(True) 'To preview...
End Sub

相关内容

  • 没有找到相关文章

最新更新