如何向 PrintPreviewControl 添加新页面并在 DataGridView 中分隔行?
How to add new page to PrintPreviewControl and separate rows in DataGridView?
我有 2 个表格:
Form1: 是我的 DataGridView 有 48 Items/Rows
Form2: 是我的 PrintPreviewControl "Dock: Fill"
现在我需要帮助来分隔 DataGridView
中的行,例如在我的 Form2 的第 1 页中添加来自 DGV 的 24 行,在第 2 页中添加其余 24 行。
我不知道如何分隔行并在 2 页中导入。
当我按原样导入行时..这些行沿着页面向下移动并且不会创建新页面(但即使有了新页面,也不知道如何分隔半行或具体数值我选成2页)
表单 1:按钮 1:Form2.show()
表格 2:
Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim x As Integer = 170
Dim y As Integer = 360
Dim xwidth As Integer = 190
Dim yheight As Integer = 20
Dim fon As New Font(FontFamily.GenericSansSerif, 12, FontStyle.Bold)
Dim rect As New Rectangle(x, 100, xwidth, yheight)
Dim rek1 As New Rectangle(40, 370, 750, 380)
e.Graphics.DrawRectangle(Pens.Black, rek1)
Dim row = Form1.DataGridView1.CurrentCell.RowIndex
e.Graphics.DrawString(Form1.DataGridView1.Item(Form1.Column1.Name, row).Value.ToString, fon, Brushes.Black, 60, 380)
e.Graphics.DrawString(Form1.DataGridView1.Item(Form1.Column2.Name, row).Value.ToString, fon, Brushes.Black, 200, 380)
e.Graphics.DrawString(Form1.DataGridView1.Item(Form1.Column3.Name, row).Value.ToString, fon, Brushes.Black, 400, 380)
End Sub
这是一个示例,说明如何从 DataGridView
:
将 24 条记录打印到一页
Private Const RECORDS_PER_PAGE As Integer = 24
Private lastRecordIndex As Integer
Private Sub PrintDocument1_BeginPrint(sender As Object, e As PrintEventArgs) Handles PrintDocument1.BeginPrint
'Reset the record index.
lastRecordIndex = -1
End Sub
Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
'Start printing at the next record or, if no records have been printed, the first record.
Dim firstRecordIndex = lastRecordIndex + 1
'The data entry row is not to be printed.
Dim printableRowCount = DataGridView1.Rows.Count - 1
'Print a page of data or the rest of the data, whichever is smaller.
lastRecordIndex = Math.Min(lastRecordIndex + RECORDS_PER_PAGE, printableRowCount - 1)
For i = firstRecordIndex To lastRecordIndex
Dim row = DataGridView1.Rows(i)
'Print row as appropriate.
Next
'Print another page if and only if there is more data.
e.HasMorePages = (lastRecordIndex < printableRowCount - 1)
End Sub
BeginPrint
事件允许您初始化与整个打印相关的任何状态 运行。 PrintPage
事件允许您打印一页数据。由您来跟踪您访问的页面。 e.HasMorePages
属性表示是否还有更多页要打印,即是否再次引发PrintPage
事件。
我有 2 个表格:
Form1: 是我的 DataGridView 有 48 Items/Rows
Form2: 是我的 PrintPreviewControl "Dock: Fill"
现在我需要帮助来分隔 DataGridView
中的行,例如在我的 Form2 的第 1 页中添加来自 DGV 的 24 行,在第 2 页中添加其余 24 行。
我不知道如何分隔行并在 2 页中导入。
当我按原样导入行时..这些行沿着页面向下移动并且不会创建新页面(但即使有了新页面,也不知道如何分隔半行或具体数值我选成2页)
表单 1:按钮 1:Form2.show()
表格 2:
Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim x As Integer = 170
Dim y As Integer = 360
Dim xwidth As Integer = 190
Dim yheight As Integer = 20
Dim fon As New Font(FontFamily.GenericSansSerif, 12, FontStyle.Bold)
Dim rect As New Rectangle(x, 100, xwidth, yheight)
Dim rek1 As New Rectangle(40, 370, 750, 380)
e.Graphics.DrawRectangle(Pens.Black, rek1)
Dim row = Form1.DataGridView1.CurrentCell.RowIndex
e.Graphics.DrawString(Form1.DataGridView1.Item(Form1.Column1.Name, row).Value.ToString, fon, Brushes.Black, 60, 380)
e.Graphics.DrawString(Form1.DataGridView1.Item(Form1.Column2.Name, row).Value.ToString, fon, Brushes.Black, 200, 380)
e.Graphics.DrawString(Form1.DataGridView1.Item(Form1.Column3.Name, row).Value.ToString, fon, Brushes.Black, 400, 380)
End Sub
这是一个示例,说明如何从 DataGridView
:
Private Const RECORDS_PER_PAGE As Integer = 24
Private lastRecordIndex As Integer
Private Sub PrintDocument1_BeginPrint(sender As Object, e As PrintEventArgs) Handles PrintDocument1.BeginPrint
'Reset the record index.
lastRecordIndex = -1
End Sub
Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
'Start printing at the next record or, if no records have been printed, the first record.
Dim firstRecordIndex = lastRecordIndex + 1
'The data entry row is not to be printed.
Dim printableRowCount = DataGridView1.Rows.Count - 1
'Print a page of data or the rest of the data, whichever is smaller.
lastRecordIndex = Math.Min(lastRecordIndex + RECORDS_PER_PAGE, printableRowCount - 1)
For i = firstRecordIndex To lastRecordIndex
Dim row = DataGridView1.Rows(i)
'Print row as appropriate.
Next
'Print another page if and only if there is more data.
e.HasMorePages = (lastRecordIndex < printableRowCount - 1)
End Sub
BeginPrint
事件允许您初始化与整个打印相关的任何状态 运行。 PrintPage
事件允许您打印一页数据。由您来跟踪您访问的页面。 e.HasMorePages
属性表示是否还有更多页要打印,即是否再次引发PrintPage
事件。