如何打印datagridview? [英] how to print datagridview ?

查看:48
本文介绍了如何打印datagridview?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在vb.net中打印datagridview的内容?
我的意思是它的列和行.

how can i print content of a datagridview in vb.net ?
i mean its columns and rows .

推荐答案

DataGridView打印 [ ^ ]


我进行了搜索,并想出了互联网上的许多链接.

试试
http://vb.net-informations.com/datagridview/vb.net_datagridview_printing.htm [ ^ ]
http://csharpdotnetfreak.blogspot.in/2012/06/print-datagridview- winforms-c-vbnet.html [ ^ ]
http://www.xmlfox.com/print_datagridview.htm [
I did a search and came up with a number of links on the internet.

Try
http://vb.net-informations.com/datagridview/vb.net_datagridview_printing.htm[^]
http://csharpdotnetfreak.blogspot.in/2012/06/print-datagridview-winforms-c-vbnet.html[^]
http://www.xmlfox.com/print_datagridview.htm[^]


'在按钮事件里调用print 类的方法
'如:
       '调用打印
        queryform.DataGrid1.DataSource = queryform.StuTable
        Dim temp As Print
        temp = New Print(queryform.DataGrid1)
        temp.setPrintFont() = New System.Drawing.Font("宋体", 12)

        temp.setPrintRecordNumber = 30
        temp.Print()

==================================================

'//print.vb
Imports System.Drawing.Printing
Public Class Print
    Private PrintFont As New Font("宋体", 10)
    Private PrintLines As Integer = 50
    Private PrintRecordNumber As Integer = 45
    Private DataGridSource As DataGrid
    Private ev As PrintPageEventArgs
    Private PrintDataGrid As PrintDocument
    Private PrintPriview As PrintPreviewDialog
    Private PageSetup As PageSetupDialog
    Private PrintScale As Double = 1
    Private DataGridColumn As DataColumn
    Private DataGridRow As DataRow
    Private DataGridTable As DataTable
    Private Cols As Integer
    Private Rows As Integer = 1
    Private ColsCount As Integer
    Private PrintingLineNumber As Integer = 0
    Private PageRecordNumber As Integer
    Dim X_unit As Integer
    Dim Y_unit As Integer
    Private PrintingPageNumber As Integer = 0
    Private PageNumber As Integer
    Private PrintRecordLeave As Integer
    Private PrintRecordComplete As Integer = 0
    Public WriteOnly Property setPrintFont() As System.Drawing.Font
        Set(ByVal Value As System.Drawing.Font)
            PrintFont = Value
        End Set
    End Property

    Public WriteOnly Property setPrintRecordNumber() As Integer
        Set(ByVal Value As Integer)
            PrintRecordNumber = Value
        End Set
    End Property

    Sub New(ByVal TableSource As DataGrid)
        DataGridSource = TableSource
        DataGridTable = New DataTable
        DataGridTable = DataGridSource.DataSource()
        ColsCount = DataGridTable.Columns.Count
    End Sub

    Public Sub Print()
        Try
            PrintDataGrid = New System.Drawing.Printing.PrintDocument
            AddHandler PrintDataGrid.PrintPage, AddressOf Me.PrintDataGrid_PrintPage
            '*
            PageSetup = New PageSetupDialog
            PageSetup.PageSettings = PrintDataGrid.DefaultPageSettings
            If PageSetup.ShowDialog() = DialogResult.Cancel Then
                Exit Sub
            End If
            '*
            If PrintDataGrid.DefaultPageSettings.Landscape = False Then
                PrintLines = PrintDataGrid.DefaultPageSettings.PaperSize.Height / (PrintFont.Height + 5)
            Else
                PrintLines = PrintDataGrid.DefaultPageSettings.PaperSize.Width / (PrintFont.Height + 5)
            End If
            '*
            If PrintDataGrid.DefaultPageSettings.PaperSize.PaperName.ToString = "custom" Then

            End If
            '*
            PrintPriview = New PrintPreviewDialog
            PrintPriview.Document = PrintDataGrid
            PrintPriview.ShowDialog()
        Catch ex As Exception
            MessageBox.Show("错误:" & ex.ToString)
        End Try
    End Sub

    Private Sub PrintDataGrid_PrintPage(ByVal sender As Object, ByVal ev As System.Drawing.Printing.PrintPageEventArgs)
        Dim strPrint As String
        Dim DrawBrush As New SolidBrush(System.Drawing.Color.Blue)
        Dim X As Integer
        Dim Y As Integer
        Dim DrawPoint As New PointF(X, Y)
        Dim row_count As Integer
        PrintRecordLeave = DataGridTable.Rows.Count - PrintRecordComplete
        '*
        PageNumber = PrintRecordLeave / PrintRecordNumber
        PrintingPageNumber = 0
        '*
        If PrintDataGrid.DefaultPageSettings.Landscape = True Then
            X_unit = PrintDataGrid.DefaultPageSettings.PaperSize.Height / (DataGridTable.Columns.Count + 2)
            Y_unit = PrintDataGrid.DefaultPageSettings.PaperSize.Width / PrintLines
        Else
            X_unit = PrintDataGrid.DefaultPageSettings.PaperSize.Width / (DataGridTable.Columns.Count + 2)
            Y_unit = PrintDataGrid.DefaultPageSettings.PaperSize.Height / PrintLines
        End If
        '*
        If DataGridTable.Rows.Count - PrintingPageNumber * PrintRecordNumber >= PrintRecordNumber Then
            PageRecordNumber = PrintRecordNumber
        Else
            PageRecordNumber = (DataGridTable.Rows.Count - PrintingPageNumber * PrintRecordNumber) Mod PrintRecordNumber
        End If

        While PrintingPageNumber <= PageNumber
            '*
            strPrint = DataGridSource.CaptionText
            DrawPoint = New PointF(X_unit, Y_unit)
            ev.Graphics.DrawString(strPrint, PrintFont, DrawBrush, DrawPoint)
            '*
            Dim ColumnText(DataGridTable.Columns.Count) As String
            Dim Table As Integer
            For Cols = 0 To DataGridTable.Columns.Count - 1
                ColumnText(Cols) = DataGridTable.Columns(Cols).ToString
                '*
                DrawPoint = New PointF(X_unit * (Cols + 1), Y_unit * 2)
                ev.Graphics.DrawString(ColumnText(Cols), PrintFont, DrawBrush, DrawPoint)

            Next
            DrawPoint = New PointF(X_unit, Y_unit * 2)
            Call drawline(DrawPoint, ev)
            '*
            Dim printingline As Integer = 0
            '*
            Dim strNonce As String = ""
            Dim strUpData As String = ""
            While printingline < PageRecordNumber
                DataGridRow = DataGridTable.Rows(PrintRecordComplete)
                '*
                For Cols = 0 To DataGridTable.Columns.Count - 1
                    DrawPoint.X = X_unit * (Cols + 1)
                    DrawPoint.Y = Y_unit * (printingline + 1 + 2)
                    If Cols = 0 Then
                        If strUpData <> "" And strNonce <> "" Then
                            If strUpData <> DataGridRow(ColumnText(0)) Then
                                ev.HasMorePages = True
                                Exit Sub
                            End If
                        End If
                    End If
                    Try
                        ev.Graphics.DrawString(DataGridRow(ColumnText(Cols)), PrintFont, DrawBrush, DrawPoint)
                        strUpData = DataGridRow(ColumnText(0))
                        '*
                    Catch
                        MsgBox(ColumnText(Cols).GetType.ToString)

                    End Try
                Next

                DrawPoint.X = X_unit * 1
                DrawPoint.Y = Y_unit * (printingline + 1 + 2)
                Call DrawLine(DrawPoint, ev)
                printingline += 1
                PrintRecordComplete += 1
                '*
                If PrintRecordComplete >= DataGridTable.Rows.Count Then
                    ev.HasMorePages = False
                    Exit Sub
                End If
            End While
            PrintingPageNumber += 1
            If PrintingPageNumber > PageNumber Then
                ev.HasMorePages = False
            Else
                ev.HasMorePages = True
                Exit While
            End If
        End While

    End Sub
    Private Sub DrawLine(ByVal point As PointF, ByVal ev As System.Drawing.Printing.PrintPageEventArgs)
        Dim blackPen As New Pen(System.Drawing.Color.Black, 1)
        ev.Graphics.DrawLine(blackPen, point.X, point.Y + PrintFont.Height, point.X * (ColsCount + 1), point.Y + PrintFont.Height)
    End Sub
End Class


这篇关于如何打印datagridview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆