如果要在vb.net中的多页上打印数据,如何打印gridview内容

时间:2014-12-25 04:11:02

标签: sql vb.net sql-server-2008 printing datagridview

我有一个gridview,它有很多数据(来自数据库)。我想把它的打印出来。以下代码工作正常,如果我只有1页打印(我的意思是只有44行记录)。如果没有打印出gridview内容的其余部分。

如果我要在多个页面上打印数据,允许多页打印需要进行哪些更改?

任何人都请帮助我......提前衷心感谢...我的代码附在下面......期待快速而明确的回答......

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

        Dim ColumnCount As Integer = DataGridView1.ColumnCount
        Dim RowCount As Integer = DataGridView1.RowCount
        Dim CellTopPos As Integer = 1

        For Row = 0 To RowCount - 1

        Dim CellLeftPos As Integer = PrintDocument1.PrinterSettings.DefaultPageSettings.Margins.Left

           For Cell = 0 To ColumnCount - 1

           Dim CellValue As String = DataGridView1.Rows(Row).Cells(Cell).Value.ToString()
           Dim CellWidth = DataGridView1.Rows(Row).Cells(Cell).Size.Width + 50
           Dim CellHeight = DataGridView1.Rows(Row).Cells(Cell).Size.Height

           Dim Brush As New SolidBrush(Color.Black)
           e.Graphics.DrawString(CellValue, New Font("Century Gothic", 10), Brush, CellLeftPos, CellTopPos)
           e.Graphics.DrawRectangle(Pens.Black, CellLeftPos, CellTopPos, CellWidth, CellHeight)

           CellLeftPos += CellWidth
           Next

           CellTopPos += DataGridView1.Rows(Row).Cells(0).Size.Height
          Next


    End sub



Private Sub Button1_Click_1(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        preview.PrintDialog1.Document = PrintDocument1
        preview.PrintDialog1.PrinterSettings = PrintDocument1.PrinterSettings
        preview.PrintPreviewControl1.Document = PrintDocument1()
        PrintDocument1.PrinterSettings.DefaultPageSettings.Landscape = False
        preview.ShowDialog()

        AddHandler PrintDocument1.PrintPage, AddressOf PrintDocument1_PrintPage
    End Sub

1 个答案:

答案 0 :(得分:0)

如果要打印更多页面并且必须在完成后将其设置为false,则将e.HasMorePages设置为true。看看Printing a DataGridView