Public Class SimpleReportTest

    Private Sub SimpleReportTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: 这行代码将数据加载到表“NorthwindDataSet.供应商”中。您可以根据需要移动或移除它。
        Me.供应商TableAdapter.Fill(Me.NorthwindDataSet.供应商)
        '构建需要的数据（表格二），表格一为自动取数
        Dim i As Integer
        Dim j As Integer
        Dim mycol As DataGridViewColumn
        Dim myrow As DataGridViewRow
        Dim rows As Integer
        Dim cols As Integer

        cols = 2
        rows = 258
        D2.ColumnCount = cols
        D2.RowCount = rows
        '加一列按钮列
        For i = 0 To cols - 1
            mycol = D2.Columns(i)
            mycol.HeaderText = "第" + CStr(i + 1) + "列"
        Next
        Dim myco2 As New DataGridViewButtonColumn()
        myco2.HeaderText = "按钮列"
        myco2.Visible = True
        myco2.DefaultCellStyle.BackColor = Color.DarkGray
        D2.Columns.Add(myco2)

        D2.Rows(2).Cells(2).Style.ForeColor = Color.Blue
        D2.Rows(2).Cells(2).Style.Font = New Font("华文行楷", 12, FontStyle.Bold Or FontStyle.Strikeout Or FontStyle.Underline)
        cols = 3

        For i = 0 To cols - 1
            mycol = D2.Columns(i)
            mycol.HeaderText = "第" + CStr(i + 1) + "列"
        Next
        For i = 0 To rows - 1
            myrow = D2.Rows(i)
            For j = 0 To cols - 1
                myrow.Cells(j).Value = "第" + CStr(i + 1) + "行 第" + CStr(j + 1) + "列"
            Next

        Next

        D2.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        D2.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight

        D2.Rows(0).Cells(0).Value = "表格2"

        Dim mycolumn As New DataGridViewCheckBoxColumn()

        'D2.Columns(2).CellTemplate
        Dim myco As New DataGridViewImageColumn()
        myco.HeaderText = "图片框"
        myco.Visible = True
        myco.ImageLayout = DataGridViewImageCellLayout.Zoom
        D2.Columns.Add(myco)
        D2.Rows(0).Cells(3).Value = My.Resources.mybmp
        D2.Rows(3).Cells(3).Value = My.Resources.myico

        mycolumn.HeaderText = "复选框"
        mycolumn.Visible = True
        mycolumn.TrueValue = "YES"
        mycolumn.FalseValue = "NO"
        D2.Columns.Add(mycolumn)
        mycolumn.ThreeState = False

        D2.Rows(0).Cells(4).Value = "True"
        D2.Rows(11).Cells(4).Value = "False"
        D2.Rows(7).Cells(4).Value = "True"
        D2.Rows(8).Cells(4).Value = "True"

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim mysp As New VBprinter.SimpleReport()
        Dim mymbfile As String
        mymbfile = My.Application.Info.DirectoryPath & "\" & "第一个打印方案.yp"
        mysp.SetReportDataSource(D1)
        mysp.IsImmediatePrint = False
        mysp.Print(mymbfile, True, True, True)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim mysp As New VBprinter.SimpleReport()
        Dim mymbfile As String
        mymbfile = My.Application.Info.DirectoryPath & "\" & "第二个打印方案.yp"
        mysp.SetReportDataSource(D2)
        mysp.IsImmediatePrint = False
        mysp.Print(mymbfile, True, True, True)
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim mysp As New VBprinter.SimpleReport()
        Dim mymbfile As String
        mymbfile = My.Application.Info.DirectoryPath & "\" & "第三个打印方案.yp"
        mysp.SetReportDataSource(D1)
        mysp.IsImmediatePrint = False
        mysp.Print(mymbfile, True, True, True)
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        '用这种方法可以将打印模板存在自己的文件或数据库中，本例以存在自己的文件中为例
        Dim myfile As String
        myfile = My.Application.Info.DirectoryPath & "\" & "自已管理模板.pp"

        Dim mycs As String
        '先从文件中读取模板内容
        If My.Computer.FileSystem.FileExists(myfile) Then
            mycs = My.Computer.FileSystem.ReadAllText(myfile)
        Else
            mycs = ""
        End If

        Dim mysp As New VBprinter.SimpleReport
        mysp.SetReportDataSource(D1)
        mysp.IsImmediatePrint = False
        mysp.Print2(mycs, True, True, True)
        If mysp.IsChanged = True Then
            '如果模板在打印预览过程中由用户改变了，则新的内容由mycs传回，直接保存mycs内容到文件中即可
            My.Computer.FileSystem.WriteAllText(myfile, mycs, False)
        End If
    End Sub
End Class