Click here to Skip to main content
13,196,126 members (49,472 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
how can i print content of a datagridview in vb.net ?
i mean its columns and rows .
Posted 5-Jun-12 2:29am
Comments
Prasad_Kulkarni 5-Jun-12 8:41am
   
What you have tried??
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

'在按钮事件里调用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
  Permalink  
v2
Comments
Member 10651672 1-Jun-14 12:13pm
   
Hello , what you mean by queryform ? its not declared

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web02 | 2.8.171019.1 | Last Updated 5 Jun 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100