Public Class Form1

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If IsNumeric(tablerows.Text) = False Then
            Exit Sub
        End If
        Try
            Dim num As Integer
            num = CInt(tablerows.Text)
            If num <= 0 Then
                Exit Sub
            End If
            dgv1.RowCount = num
            For i As Integer = 0 To dgv1.RowCount - 1
                dgv1.Rows(i).Cells(0).Value = CStr(i + 1)
            Next
            Dim myrnd As New Random

            For i = 0 To dgv1.RowCount - 1
                For j As Integer = 1 To dgv1.ColumnCount - 2
                    dgv1.Rows(i).Cells(j).Value = "第" & CStr(i + 1) & "行第" & CStr(j) & "列"
                Next
                dgv1.Rows(i).Cells(dgv1.ColumnCount - 1).Value = myrnd.Next(100)
            Next
        Catch ex As Exception

        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        myp.NewDoc()
        Dim mydgv As New DataGridView()
        myp.CopyDataGridView(dgv1, mydgv)
        Dim sumrowheight As Single '合计行要打印的行高
        sumrowheight = myp.ConvFromDisplay(mydgv.Rows(0).Height, False)
        Dim isneednewpage As Boolean = True
        Dim startrow As Integer
        Dim endrow As Integer
        startrow = 0
        For i As Integer = 0 To mydgv.RowCount - 1
            If isneednewpage = True Then
                myp.NewPage()
                DrawTitle()
                myp.DrawDGVHeader(mydgv, True, True, "1111", 0)
                isneednewpage = False
            End If
            If myp.IsNewPage(myp.ConvFromDisplay(mydgv.Rows(i).Height, False)) = True Then
                '打印合计数
                endrow = i - 1
                PrintSumRow(mydgv, startrow, endrow, sumrowheight, "本页小计")
                startrow = i
                isneednewpage = True
                i = i - 1
            Else
                myp.DrawDGVRow(mydgv, i, True, False, "1111", 0)
            End If
        Next
       
       
            PrintSumRow(mydgv, 0, mydgv.RowCount - 1, sumrowheight, "总计")

        myp.EndDoc("打印预览")
    End Sub
    Private Sub PrintSumRow(ByVal dgv As DataGridView, ByVal startrow As Integer, ByVal endrow As Integer, ByVal rowheight As Decimal, ByVal ss As String)
        Dim je As Decimal
        je = 0
        For i As Integer = startrow To endrow
            je = je + CDec(dgv.Rows(i).Cells(6).Value)
        Next
        Dim mys As String
        mys = ss & ":" & CStr(je)
        myp.PrintFooter("", mys, "", New Font("黑体", 12, FontStyle.Bold))
    End Sub

    Private Sub DrawTitle()
        Dim myfont As New Font("黑体", 18, FontStyle.Bold)
        Dim mytitle As String = "这是表格的标题"
        myp.DrawTitle(mytitle, myfont, Color.Blue, 0)
        myp.NewRow(myp.LastDrawTextHeight)

    End Sub



    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Button3_Click(Nothing, Nothing)
    End Sub

    Private Sub myp_HeaderFooterOut(ByVal pages As System.Int32, ByVal curpage As System.Int32) Handles myp.HeaderFooterOut
        Dim mys As String
        mys = "第" & CStr(curpage) & "页 总共" & CStr(pages) & "页"
        Dim myfont As New Font("宋体", 12)
        myp.SetCurrentPage(curpage)
        myp.Currentx = 0
        myp.Currenty = 0

        myp.DrawText(mys, myp.PaperPrintWidth, 50, myfont, Color.Red, StringAlignment.Far, StringAlignment.Far, False, False, False, False, 0, 0)
    End Sub
End Class
