我有以下代码来打印凭证,但是如果页面已满,则在打印SQL查询结果时遇到循环部分问题,它会使用同一页面重新打印。 enter image description here
Private Sub ATATprint_PrintPage(sender As Object, e As PrintPageEventArgs) Handles ATATprint.PrintPage
Dim Brush1 As New SolidBrush(Color.Black)
Dim ValueBrush As New SolidBrush(Color.DarkGreen)
Dim lblFont As Font = New Font("B Yekan", 10, FontStyle.Regular)
Dim ValueFont As Font = New Font("Agency FB", 10, FontStyle.Bold)
Dim ypos As Integer = 300
Dim pn As Integer = 1
Dim str(6) As String
str(0) = TrnAccountType
str(1) = TrnAccountNo
str(2) = TrnAccountName
str(3) = TrnCurrecy
str(4) = TrnExRate
str(5) = TrnAmount
str(6) = TrnNarration
Try
Dim adapter As New SqlDataAdapter("select case when trd_DrCr = 'Dr' then 'Debit' else 'Credit' end,
isnull(acc_Ccy, '')+'-'+Convert(nvarchar,trd_Account), acc_Name, trd_ccy, format(trd_ExRate,'#,###,###.0000'), format(trd_Amount, '#,###,###.00'), trd_Narration
from TransactionDetails join Accounts on Accounts.acc_Number = TransactionDetails.trd_Account where trd_TrnRef = '" & fncTrnReference.Text & "'", connection)
Dim table As New DataTable
adapter.Fill(table)
For row As Integer = 0 To table.Rows.Count - 1
For col As Integer = 0 To table.Columns.Count - 1
e.Graphics.DrawString(str(col), lblFont, Brush1, 100, ypos)
e.Graphics.DrawString(table.Rows(row)(col).ToString, ValueFont, ValueBrush, 200, ypos)
ypos += 15
Next
ypos += 30
If ypos > 900 Then
ypos = 200
e.HasMorePages = True
End If
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
e.HasMorePages = False
End Sub
答案 0 :(得分:0)
您需要在PrintPage方法之外填充适配器,并且还要从for循环中跟踪行变量的值。再次调用PrintPage方法时,从该点开始For循环。
此外,如果您使用的是标准颜色,则无需创建新的SolidColorBrushes。
最后,确保完成后处置字体。我是在下面的代码的Final代码块中完成的。
此代码未经测试,因此可能需要进行一些更改以满足您的需求,但是它应该可以为您提供一些思路。
'These variables are declared outside the PrintPage method so they retain their values between calls.
Private adapter As SqlDataAdapter = Nothing
Private savedRowValue As Integer = 0
Private table As DataTable = Nothing
Private Sub ATATprint_PrintPage(sender As Object, e As PrintPageEventArgs) Handles ATATprint.PrintPage
Dim Brush1 As Brushes.Black 'Use the standard brushes here.
Dim ValueBrush Brushes.DarkGreen
Dim lblFont As Font = New Font("B Yekan", 10, FontStyle.Regular)
Dim ValueFont As Font = New Font("Agency FB", 10, FontStyle.Bold)
Dim ypos As Integer = 300
Dim pn As Integer = 1
Dim str(6) As String
str(0) = TrnAccountType
str(1) = TrnAccountNo
str(2) = TrnAccountName
str(3) = TrnCurrecy
str(4) = TrnExRate
str(5) = TrnAmount
str(6) = TrnNarration
Try
'Create the data adapter and fill the DataTable only on the first time the PrintPage method is called
If adapter Is Nothing Then
adapter As New SqlDataAdapter("select case when trd_DrCr = 'Dr' then 'Debit' else 'Credit' end,
isnull(acc_Ccy, '')+'-'+Convert(nvarchar,trd_Account), acc_Name, trd_ccy, format(trd_ExRate,'#,###,###.0000'), format(trd_Amount, '#,###,###.00'), trd_Narration
from TransactionDetails join Accounts on Accounts.acc_Number = TransactionDetails.trd_Account where trd_TrnRef = '" & fncTrnReference.Text & "'", connection)
table = New DataTable
adapter.Fill(table)
End If
'Start the For loop at the saved value instead of at 0
For row As Integer = savedRowValue To table.Rows.Count - 1
For col As Integer = 0 To table.Columns.Count - 1
e.Graphics.DrawString(str(col), lblFont, Brush1, 100, ypos)
e.Graphics.DrawString(table.Rows(row)(col).ToString, ValueFont, ValueBrush, 200, ypos)
ypos += 15
Next
ypos += 30
If ypos > 900 Then
ypos = 200
e.HasMorePages = True
savedRowValue = row + 1 'Save the value of the current row
Exit Sub
End If
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
lblFont.Dispose() 'Dispose of your fonts here
ValueFont.Dispose()
End Try
e.HasMorePages = False
End Sub