在表格中打印dataGridView(vb2010)

时间:2015-10-13 09:32:32

标签: vb.net

我是vb编程的新手。我正在使用vb2010我正在做一个数据库应用程序。它工作正常。但我必须添加另一项任务。那就是以表格形式打印DGV上的数据。我已经做了很多关于此的研究,但还没有找到它。任何帮助或回应将受到高度赞赏。

对不起,这是我的代码:它有效,但它只打印一个黑色文档。

Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\Users\IT-PC\Documents\Visual Studio 2010\Projects\MaterialSectionIS\MaterialSectionIS\My Project\materialsInventory.accdb"

    Dim MyPrintDocument As PrintDocument = New PrintDocument
    Dim MyDataGridViewPrinter As DataGridViewPrinter
    Dim MyPrintDialog As PrintDialog = New PrintDialog()

    MyPrintDialog.AllowCurrentPage = False
    MyPrintDialog.AllowPrintToFile = False
    MyPrintDialog.AllowSelection = False
    MyPrintDialog.AllowSomePages = True
    MyPrintDialog.PrintToFile = False
    MyPrintDialog.ShowHelp = False
    MyPrintDialog.ShowNetwork = False
    'Dim commdata As New OleDb.OleDbCommand("SELECT * FROM metal WHERE itemCodeM = @tbItemCodeMetal", myConnection)
    Dim MyConn As OleDbConnection
    Dim da As OleDbDataAdapter
    Dim ds As DataSet
    Dim tables As DataTableCollection
    Dim source1 As New BindingSource

    MyConn = New OleDbConnection
    MyConn.ConnectionString = connString
    ds = New DataSet
    tables = ds.Tables
    da = New OleDbDataAdapter("Select * from [metal]", MyConn)
    da.Fill(ds, "metal")
    Dim view As New DataView(tables(0))
    source1.DataSource = view
    DataGridView1.DataSource = view
    If MyPrintDialog.ShowDialog() <> System.Windows.Forms.DialogResult.OK Then Return False

    MyPrintDocument.DocumentName = "metal"
    MyPrintDocument.PrinterSettings = MyPrintDialog.PrinterSettings
    MyPrintDocument.DefaultPageSettings = MyPrintDialog.PrinterSettings.DefaultPageSettings
    MyPrintDocument.DefaultPageSettings.Margins = New Margins(40, 40, 40, 40)

    If MessageBox.Show("Do you want the report to be centered on the page", "METAL - Center on Page", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
        MyDataGridViewPrinter = New DataGridViewPrinter(DataGridView1, MyPrintDocument, True, True, "metal", New Font("Tahoma", 18, FontStyle.Bold, GraphicsUnit.Point), Color.Black, True)
    Else
        MyDataGridViewPrinter = New DataGridViewPrinter(DataGridView1, MyPrintDocument, False, True, "metal", New Font("Tahoma", 18, FontStyle.Bold, GraphicsUnit.Point), Color.Black, True)
    End If

    Return True
End Function


Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    Dim more As Boolean

    Try
        more = MyDataGridViewPrinter.DrawDataGridView(e.Graphics)
        If more Then e.HasMorePages = True
    Catch Ex As Exception
        MessageBox.Show(Ex.Message & vbCrLf & Ex.StackTrace, MyConstants.CaptionFehler, MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim MyPrintDocument As PrintDocument = New PrintDocument

    If SetupThePrinting() Then
        Dim MyPrintPreviewDialog As PrintPreviewDialog = New PrintPreviewDialog()
        MyPrintPreviewDialog.Document = MyPrintDocument
        MyPrintPreviewDialog.ShowDialog()
    End If
End Sub





Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim MyPrintDocument As PrintDocument = New PrintDocument
    If SetupThePrinting() Then MyPrintDocument.Print()
End Sub

0 个答案:

没有答案