DataGrid控制元件通用列印類. (轉)

amyz發表於2007-08-15
DataGrid控制元件通用列印類. (轉)[@more@]


Imports System.Drawing.Printing
Imports System.Drawing.Color
Imports System..Forms
Imports System.Drawing.Font
Imports System.Drawing.PointF
Imports System.Windows.Forms.DataGrid
Imports System.Drawing.Pen
Imports System.Drawing

 

''=======================================
'DATAGRID通用列印類
'中和科技-孫利臣
'於年05月27日17:05
'用於列印DATAGRID控制元件中的資料.

'=======================================


Public Class PrintDataGrid
  '可自定義
  Private PrintFont As New Font("宋體", 10)  '當前要列印文字的字型及字號
  Private PrintLines As Integer = 50  '當前頁共要分成多少行.
  Private PrintRecordNumber As Integer = 45  '當前頁共要列印的記錄的行數

  '以下為模組內部使用
  Private DataGrid As DataGrid
  Private ev As PrintPageEventArgs
  Private PrintDataGrid As PrintDocument
  Private PrintPriview As PrintPreviewDialog
  Private PageSetup As PageSetupDialog
  Private PrintScale As Double = 1  '當前要列印的資料與DATAGRID控制元件內資料的比例

  Private DataGridColumn As DataColumn
  Private DataGridRow As DataRow
  Private DataGridTable As DataTable

  Private Cols As Integer  '當前要列印的列
  Private Rows As Integer = 1  '當前要列印的行

  Private Colunt As Integer  '當前DATAGRID共有多少列

  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  '已經列印完的記錄數

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

  '使用者自定義字型及字號
  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


  Public Sub Print()
  Try
  PrintDataGrid = New System.Drawing.Printing.PrintDocument()
  AddHandler PrintDataGrid.PrintPage, AddressOf Me.PrintDataGrid_PrintPage
  'PrintDataTable.Print()

  '印表機設定對話方塊
  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


  '如果使用者選擇自定義紙張大小列印,則按B5紙列印,不管實際紙張大小
  If PrintDataGrid.DefaultPageSettings.PaperSize.PaperName.ToString = "custom" Then

  End If

  '預覽視窗
  PrintPriview = New PrintPreviewDialog()
  PrintPriview.Document = PrintDataGrid
  PrintPriview.ShowDialog()

  Catch ex As Exception
  MessageBox.Show("error:" & ex.ToString)
  Finally

  End Try
  End Sub

  Private Sub PrintDataGrid_PrintPage(ByVal sender As , ByVal ev As System.Drawing.Printing.PrintPageEventArgs)

  'A4紙  A4 紙,210 x 297 毫米。
  'B5紙 B5 紙,182 x 257 毫米。

  Dim strPrint As String  '當前要列印的文字
  Dim DrawBrush As New Solirush(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  '因為每列印一個新頁都要計算還有多少頁沒有列印所以以列印的頁數初始為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)
  '文件標題----------------列印結束

  '得到DATAGRID的所有列名
  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)  '畫線
  '結束---------------------得到DATAGRID的所有列名


  Dim PrintingLine As Integer = 0  '當前頁面已經列印的記錄行數

  '用於確定是否換頁的標記
  Dim strUpData As String = ""  '當前資料的前一個資料
  Dim strNonce 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 <> "" Then  'And strNonce <> ""
  If strUpData <> DataGridRow(ColumnText(0)) Then
  ev.HasMorePages = True
  Exit Sub

  End If
  End If
  End If

  ev.Graphics.DrawString(DataGridRow(ColumnText(Cols)), PrintFont, DrawBrush, DrawPoint)
  strUpData = DataGridRow(ColumnText(0))  '當前行資料列印完成後,將打記錄的第一列儲存,(也可不用此語句,只為明確)
  Next

  DrawPoint.X = X_unit * 1
  DrawPoint.Y = Y_unit * (PrintingLine + 1 + 2)

  Call DrawLine(DrawPoint, ev)

  PrintingLine += 1
  PrintRecordComplete += 1

  '列印完最後一條記錄後結束列印.
  '如:當前有500條記錄.從0開始列印,實際列印的為第一條記錄.則列印500條時實際的是第501條記錄.也就是最後一條
  'datagridtable.rows.count得到就是表內的實際記錄條數,共有多少條記錄(從1開始),當
  'printrecordcomplete>=datagridtable.rows.count也就是當前已經列印到了500條,加1後將要列印第501條,越界,則結束.
  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

 

  '畫線  只必指定當前行的列印文字的開始位置就可,x,y為當前行文字的列印位置
  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

 

 

``````````````````````````````````````````````````````

  Private aa As PrintDataGrid.PrintDataGrid

  aa = New PrintDataGrid.PrintDataGrid(DataGrid1)

  aa.setPrintFont() = New System.Drawing.Font("宋體", 12)  '列印文字的字型
  aa.setPrintRecordNumber = 30  '每頁要列印的記錄行數

  aa.Print()

 

功能:可以直接列印DATAGRID控制元件中的資料,但不能對資料進行格式化,功能還在完善中:)

希望大家多多指教.謝謝.

 


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752019/viewspace-958203/,如需轉載,請註明出處,否則將追究法律責任。

相關文章