如何将新页面添加到 PrintPreviewControl 并在 DataGridView 中分隔行



>我有 2 种形式:

Form1:是我的数据网格视图,包含 48 个项目/行

Form2:是我的打印预览控件,带有"停靠:填充"

现在我需要帮助来分隔DataGridView中的行,例如在我的 Form1 中添加第 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

下面是如何将 24 条记录从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事件。

相关内容

最新更新