⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datagridprinter.vb

📁 使用Access数据库演示的任务分配管理程序 一个使用ADO.NET基于Microsoft Access数据库演示的任务分配管理的程序
💻 VB
字号:
'---------------------------------------------------------------------
'  This file is part of the Microsoft .NET Framework SDK Code Samples.
' 
'  Copyright (C) Microsoft Corporation.  All rights reserved.
' 
' This source code is intended only as a supplement to Microsoft
' Development Tools and/or on-line documentation.  See these other
' materials for detailed information regarding Microsoft code samples.
' 
' THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
' KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
' IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
' PARTICULAR PURPOSE.
'---------------------------------------------------------------------

Imports System.Drawing.Printing

Public Class DataGridPrinter

    Public RowCount As Integer = 0
    Public PageNumber As Integer = 1

    Private m_DataTable As DataTable
    Private m_DataGridView As DataGridView
    Private m_ImageArray(2) As Image

    Private m_PageWidth As Integer
    Private m_PageWidthMinusMargins As Integer
    Private m_PageHeight As Integer
    Private m_AdjColumnBy As Integer
    Private m_IsTooWide As Boolean
    Private m_DataGridViewWidth As Integer

    Private Const c_TopMargin As Integer = 50
    Private Const c_BottomMargin As Integer = 50
    Private Const c_LeftMargin As Integer = 50
    Private Const c_RightMargin As Integer = 50
    Private Const c_VerticalCellLeeway As Integer = 10

    Public Sub New(ByVal dgv As DataGridView, ByVal pd As PrintDocument, ByVal dt As DataTable)
        m_DataGridView = dgv
        m_DataTable = dt

        'set the document as landscape
        pd.DefaultPageSettings.Landscape = True

        'extract our width and height values
        m_PageHeight = pd.DefaultPageSettings.PaperSize.Width
        m_PageWidth = pd.DefaultPageSettings.PaperSize.Height
        m_PageWidthMinusMargins = m_PageWidth - (c_LeftMargin + c_RightMargin)

        'hard-coded images
        m_ImageArray(0) = My.Resources.Major
        m_ImageArray(1) = My.Resources.Medium
        m_ImageArray(2) = My.Resources.Minor

        m_DataGridViewWidth = GetDataGridViewWidth()

        'set up some adjustments to scale the output later
        If m_DataGridView.Width > m_PageWidthMinusMargins Then
            m_AdjColumnBy = m_DataGridView.Width - m_PageWidthMinusMargins
            m_DataGridViewWidth = m_DataGridViewWidth - m_AdjColumnBy
            m_IsTooWide = True
        Else
            m_AdjColumnBy = m_PageWidthMinusMargins - m_DataGridView.Width
            m_DataGridViewWidth = m_DataGridViewWidth + m_AdjColumnBy
            m_IsTooWide = False
        End If
    End Sub

    Public Function DrawDataGridView(ByVal g As Graphics) As Boolean
        Try
            DrawPageHeader(g)
            Return DrawPageRows(g)
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString())
            Return False
        End Try
    End Function

    Private Sub DrawPageHeader(ByVal g As Graphics)
        'create the header rectangle
        Dim headerBounds As New RectangleF(c_LeftMargin, c_TopMargin, m_PageWidthMinusMargins, m_DataGridView.ColumnHeadersDefaultCellStyle.Font.SizeInPoints + c_VerticalCellLeeway)

        'draw the header rectangle
        g.FillRectangle(New SolidBrush(m_DataGridView.ColumnHeadersDefaultCellStyle.BackColor), headerBounds)

        Dim xPosition As Single = c_LeftMargin + 12 ' +12 for some padding

        'use this format when drawing later
        Dim cellFormat As New StringFormat()
        cellFormat.Trimming = StringTrimming.Word
        cellFormat.FormatFlags = StringFormatFlags.NoWrap Or StringFormatFlags.LineLimit

        'find the column names from the tablestyle
        Dim col As DataGridViewColumn
        For Each col In m_DataGridView.Columns
            If col.Visible = True Then

                'temp width to draw this column
                Dim columnWidth As Integer = col.Width

                'scale the summary column width
                'note: just a quick way to fit the text to the page width
                'this is not the best way to do this but it handles the most
                'common ui path for this demo app
                If col.Name = "Summary" And m_IsTooWide Then
                    columnWidth -= m_AdjColumnBy
                ElseIf col.Name = "Summary" Then
                    columnWidth += m_AdjColumnBy
                End If

                'create a layout rectangle to draw within.
                Dim cellBounds As New RectangleF(xPosition, c_TopMargin, columnWidth, m_DataGridView.ColumnHeadersDefaultCellStyle.Font.SizeInPoints + c_VerticalCellLeeway)

                'draw the column name
                g.DrawString(col.HeaderText, m_DataGridView.ColumnHeadersDefaultCellStyle.Font, New SolidBrush(m_DataGridView.ColumnHeadersDefaultCellStyle.ForeColor), cellBounds, cellFormat)

                'adjust the next X Pos
                xPosition += columnWidth
            End If
        Next
    End Sub

    Private Function DrawPageRows(ByVal g As Graphics) As Boolean

        Dim yPosition As Single = c_TopMargin + m_DataGridView.ColumnHeadersDefaultCellStyle.Font.SizeInPoints + (c_VerticalCellLeeway * 2)

        'use this format when drawing later
        Dim cellFormat As New StringFormat()
        cellFormat.Trimming = StringTrimming.Word
        cellFormat.FormatFlags = StringFormatFlags.NoWrap Or StringFormatFlags.LineLimit

        'loop each visible row
        Dim i As Integer = 0
        For i = RowCount To (m_DataTable.DefaultView.Count - 1)

            Dim xPosition As Single = c_LeftMargin + 12 ' +12 for some padding

            'loop the columns of this row, if the column is visible
            'then print the cell value
            Dim col As DataGridViewColumn
            For Each col In m_DataGridView.Columns
                If col.Visible = True Then

                    'temp width to draw this column
                    Dim columnWidth As Integer = col.Width

                    'scale the summary column width
                    'note: just a quick way to fit the text to the page width
                    'this is not the best way to do this but it handles the most
                    'common ui path for this demo app
                    If col.Name = "Summary" And m_IsTooWide Then
                        columnWidth -= m_AdjColumnBy
                    ElseIf col.Name = "Summary" Then
                        columnWidth += m_AdjColumnBy
                    End If

                    'create a layout rectangle to draw within.
                    Dim cellBounds As New RectangleF(xPosition, yPosition, columnWidth, m_DataGridView.Font.SizeInPoints + c_VerticalCellLeeway)

                    'draw the item value
                    If col.Name = "PriorityText" Then
                        'draw image
                        Select Case m_DataTable.DefaultView.Item(i).Item("PriorityText")
                            Case "Major"
                                g.DrawImage(m_ImageArray(0), New Point(Convert.ToInt32(cellBounds.X) - 5, Convert.ToInt32(cellBounds.Y)))
                            Case "Medium"
                                g.DrawImage(m_ImageArray(1), New Point(Convert.ToInt32(cellBounds.X) - 5, Convert.ToInt32(cellBounds.Y)))
                            Case "Minor"
                                g.DrawImage(m_ImageArray(2), New Point(Convert.ToInt32(cellBounds.X) - 5, Convert.ToInt32(cellBounds.Y)))
                        End Select
                    Else
                        'draw as short date format or regular string
                        If m_DataTable.DefaultView.Item(i).Item(col.Name).GetType() Is GetType(DateTime) Then
                            g.DrawString(String.Format("{0:d}", m_DataTable.DefaultView.Item(i).Item(col.Name)), m_DataGridView.Font, New SolidBrush(m_DataGridView.ColumnHeadersDefaultCellStyle.ForeColor), cellBounds, cellFormat)
                        Else
                            g.DrawString(CType(m_DataTable.DefaultView.Item(i).Item(col.Name), String), m_DataGridView.Font, New SolidBrush(m_DataGridView.ColumnHeadersDefaultCellStyle.ForeColor), cellBounds, cellFormat)
                        End If
                    End If

                    'adjust the next X Pos
                    xPosition += columnWidth
                End If
            Next

            'set the rowcount (which is used for a possible next page)
            RowCount += 1

            'finished with this row, draw a nice line
            g.DrawLine(New Pen(m_DataGridView.GridColor, 1), c_LeftMargin, yPosition + m_DataGridView.ColumnHeadersDefaultCellStyle.Font.SizeInPoints + c_VerticalCellLeeway - 2, m_PageWidthMinusMargins + c_LeftMargin, yPosition + m_DataGridView.ColumnHeadersDefaultCellStyle.Font.SizeInPoints + c_VerticalCellLeeway - 1)

            'adjust the next Y Pos
            yPosition += m_DataGridView.ColumnHeadersDefaultCellStyle.Font.SizeInPoints + c_VerticalCellLeeway + 3

            'if at end of page exit out for next page
            If yPosition * PageNumber > PageNumber * (m_PageHeight - (c_BottomMargin + c_TopMargin)) Then
                Return True
            End If
        Next

        Return False
    End Function

    Private Function GetDataGridViewWidth() As Integer
        Try
            Dim col As DataGridViewColumn
            Dim dgWidth As Integer = 0
            For Each col In m_DataGridView.Columns
                If col.Visible = True Then
                    dgWidth = dgWidth + col.Width
                End If
            Next
            Return dgWidth
        Catch ex As Exception
            LogError.Write(ex.Message & vbNewLine & ex.StackTrace)
            Throw ex
        End Try
    End Function
End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -