Public Class printdgvselect
    Dim printtype As Integer
   
    Private Sub printdgvselect_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.客户TableAdapter.Fill(Me.NorthwindDataSet.客户)
        D1.DataSource = Me.NorthwindDataSet.客户

        '表格字段名特殊格式设置打印测试
        D1.Columns(1).HeaderCell.Style.ForeColor = Color.Red
        Dim mystyle As FontStyle
        mystyle = FontStyle.Bold + FontStyle.Strikeout + FontStyle.Underline + FontStyle.Italic
        D1.Columns(1).HeaderCell.Style.BackColor = Color.Blue
        D1.Columns(1).HeaderCell.Style.Font = New Font("黑体", 13, mystyle)
        D1.Columns(3).HeaderCell.Style.BackColor = Color.Red





        '构建测试数据
        dgv1.RowCount = 40
        For i As Integer = 0 To dgv1.RowCount - 1
            dgv1.Rows(i).Cells(0).Value = CStr(i + 1)
            dgv1.Rows(i).Cells(1).Value = "电缆"
            dgv1.Rows(i).Cells(3).Value = "米"
        Next
        For i As Integer = 0 To dgv1.RowCount - 1
            dgv1.Rows(i).Cells(2).Value = 50 + i
        Next

        dgv1.Rows(0).Cells(4).Value = 10
        dgv1.Rows(0).Cells(5).Value = 50
        dgv1.Rows(0).Cells(6).Value = 500

        dgv1.Columns(6).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        dgv1.Columns(5).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight


        dgv1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
        dgv1.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCells)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim mytitle As String = ""
        If RadioButton1.Checked = True Then
            DgVprint1.PrintRange = VBprinter.DGVprint.DGVPrintRange.AllVisibleRowsAndColumns
            mytitle = "打印DGV中选中内容的演示（打印范围为所有可见的行与列）"
        ElseIf RadioButton2.Checked = True Then
            DgVprint1.PrintRange = VBprinter.DGVprint.DGVPrintRange.SelectedColumns
            mytitle = "打印DGV中选中内容的演示（打印范围为选定列的全部行）"
        ElseIf RadioButton3.Checked = True Then
            DgVprint1.PrintRange = VBprinter.DGVprint.DGVPrintRange.SelectedRows
            mytitle = "打印DGV中选中内容的演示（打印范围为选定行的全部列）"
        ElseIf RadioButton4.Checked = True Then
            DgVprint1.PrintRange = VBprinter.DGVprint.DGVPrintRange.SelectedRowsAndColumns
            mytitle = "打印DGV中选中内容的演示（打印范围为选中的行与列的区域）"
        End If
        DgVprint1.PaperLandscape = True '横向打印
        DgVprint1.PrintType = VBprinter.DGVprint.mytype.GeneralPrint
        DgVprint1.Alignment = StringAlignment.Center '表格居中
        DgVprint1.PaperMargins = New System.Drawing.Printing.Margins(150, 140, 150, 150)
        DgVprint1.SubTitle = "表格小标题"
        DgVprint1.MainTitle = mytitle
        DgVprint1.PageFooterLeft = "页脚内容（左边）"
        DgVprint1.PageFooterRight = "页脚内容（右边）"
        DgVprint1.Print(dgv1, False)

    End Sub

   
  

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim mytitle As String = ""
        Dim newdgv As New DataGridView

        If RadioButton1.Checked = True Then
            myp.CopyDataGridView(dgv1, newdgv, False)
            mytitle = "打印DGV中选中内容的演示（打印范围为所有可见的行与列）"
        ElseIf RadioButton2.Checked = True Then
            myp.CopyDataGridViewSelectedRange(dgv1, newdgv, VBprinter.VB2008Print.DGVCopyRange.SelectedColumns)
            mytitle = "打印DGV中选中内容的演示（打印范围为选定列的全部行）"
        ElseIf RadioButton3.Checked = True Then
            myp.CopyDataGridViewSelectedRange(dgv1, newdgv, VBprinter.VB2008Print.DGVCopyRange.SelectedRows)
            mytitle = "打印DGV中选中内容的演示（打印范围为选定行的全部列）"
        ElseIf RadioButton4.Checked = True Then
            myp.CopyDataGridViewSelectedRange(dgv1, newdgv, VBprinter.VB2008Print.DGVCopyRange.SelectedRowsAndColumns)
            mytitle = "打印DGV中选中内容的演示（打印范围为选中的行与列的区域）"
        End If

        myp.NewDoc()
        myp.NewPage(True)
        myp.PrintDGV(newdgv, mytitle, New Font("黑体", 18), "", Nothing, StringAlignment.Center, "1111", True, False, Nothing, "", "", "", Nothing, "", "", "", 6, True, True, 0, 0, False, Nothing, StringAlignment.Center, StringAlignment.Center, StringAlignment.Center, StringAlignment.Center, StringAlignment.Center, StringAlignment.Center)
        myp.EndDoc()
    End Sub


 

   
End Class