Imports System.Drawing.PrintingImports System.Drawing.ColorImports System.Windows.FormsImports System.Drawing.FontImports System.Drawing.PointFImports System.Windows.Forms.DataGridImports System.Drawing.PenImports System.Drawing
'================================================= DataGrid control universal print class' Technology - Sun Lichen 'on May 27, 2003 17:05' used to print data in the DataGrid control.
'========================================
Public Class PrintDataGrid 'User Customizers Private PrintFont AS New Font ("Song", 10)' Current PRINTLINES AS INTEGER = 50 'Current page A total of how many rows. Private printRecordNumber as integer = 45 'The number of records of the record of the current page
'The following is an internal module 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' the current data to be printed and the data control ratio DATAGRID
Private DataGridColumn As Datacolumn Private DataGridrow As DataRow Private DataGridtable as DataTable
Private cols as integer 'Current Rows as INTEGER = 1' Currently Printing Row
PRIVATE COLSCOUNT AS INTEGER 'How many columns are currently DataGrid?
Private PrintingLinenumber AS Integer = 0 'Current PRIVATE PAGERECORDNUMBER AS INTEGER' The number of records currently wants to print, is calculated to get the .dim x_unit as integer 'table of the basic unit DIM Y_Unit AS Integer
Private printingpagenumber as integer = 0 'Page number to print
Private Pagenumber As Integer 'Top number of pages in need Print Private PrintRecordleave as integer' How many pages are currently printed printed Private PrintRecordComplete as integer = 0 'has already printed the number of records
Sub New (ByVal TableSource As DataGrid) DataGridSource = TableSource DataGridTable = New DataTable () DataGridTable = DataGridSource.DataSource () ColsCount = DataGridTable.Columns.Count End Sub
'User Custom Fonts and Dictionary Public Writeonly Property SetPrintFont () AS System.drawing.Font Set (Byval Value As System.drawing.Font) PrintFont = Value End SET End Property
'Set the number of records to be printed per page PUBLIC WRITEONLY Property SetPrinTrecordNumber () AS Integer Set (BYVAL VALUE AS INTEGER) PrintNumber = Value End SET End Property
Public Sub Print () Try PrintDataGrid = New System.drawing.printing.printDocument () AddHandler PrintDataGrid.printpage, Addressof Me.PrintDataGrid_printpage 'PrintDataTable.print ()
'Printer Settings Dialog Pagesetup = New PagesetupDialog () Pagesetup.pagesettings = PrintDataGrid.defaultpagesettings if Pagesetup.showDialog () = DialogResult.cancel kil dam Endiff.cancel kil1n
'This page is landscape or portrait print' count the rows If PrintDataGrid.DefaultPageSettings.Landscape this page can be printed in total = False Then PrintLines = PrintDataGrid.DefaultPageSettings.PaperSize.Height / (PrintFont.Height 5) Else PrintLines = PrintDataGrid.DefaultPageSettings .PaPersize.width / (PrintFont.Height 5) end if 'If the user selects a custom paper size print, press B5 paper, regardless of the actual paper size if printdataGrid.defaultpagesettings.papersize.papername.tostring = "Custom" THEN
END IF
'Preview window printpriView = new printpreviewDialog () printpriView.document = printDataGrid printpriView.showdialog ()
Catch exception messageBox.show ("Error:" & ex.totostring) Finally
END TRY END SUB
Private sub printDataGrid_printpage (Byval EV as system.drawing.printing.printpageEventArgs)
'A4 paper A4 paper, 210 x 297 mm. 'B5 paper B5 paper, 182 x 257 mm.
DIM STRINT AS STRING 'Current Dim Drawbrush As New SolidBrush (System.drawing.Color.blue)' Current Brush Color
DIM X AS Integer 'Currently Current Sidal Dim Y AS Integer' The column coordinates of the text you want to print.
Dim DrawPoint As New Pointf (x, y)
DIM ROW_COUNT AS INTEGER 'Currently to be printed
PrintRecordleave = DataGridTable.Rows.count - How many records do not print in PrintRecordComplete '
Pagenumber = prinTrecordleave / printRecordnumber 'Tset page number printingpagenumber = 0' Because each printing a new page must calculate how many pages are not printed, so the number of pages of printing is 0
'The basic unit of this page into If PrintDataGrid.DefaultPageSettings.Landscape = True Then X_unit = PrintDataGrid.DefaultPageSettings.PaperSize.Height / (DataGridTable.Columns.Count 2) Y_unit = PrintDataGrid.DefaultPageSettings.PaperSize.Width / PrintLinesElse X_unit = PrintDataGrid .Defaultpagesettings.papersize.width / (DataGridTable.column.count 2) y_Unit = printDataGrid.defaultpagesettings.papersize.height / Printlines
END IF
'Calculated, whether the remaining number of records may be printed on one page, is less than when a false If DataGridTable.Rows.Count - PrintingPageNumber * PrintRecordNumber> = PrintRecordNumber Then PageRecordNumber = PrintRecordNumber Else PageRecordNumber = (DataGridTable.Rows.Count - PrintingPageNumber * PrintRecordNumber) MOD PrintRecordNumber End IF
While PrintingPagenumber <= Pagenumber
'Document Title ---------------- Print start strprint = DataGridSource.captiontext' Document title DrawPoint = New Pointf (x_unit, y_Unit) ev.Graphics.drawstring (Strprint, PrintFont, Drawbrush, Drawpoint) 'Document Title ---------------- Print End
'Get all column name 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 'get all the column names
DrawPoint = New Pointf (x_unit * (COLS 1), y_Unit * 2) ev.Graphics.drawstring (ColumnText (Colus), PrintFont, Drawbrush, DrawPoint
NEXT
DrawPoint = New Pointf (x_unit, y_Unit * 2) Call Drawline (DrawPoint, EV) 'Page' end -------------------- get all the column names from DataGrid DIM PrintingLine AS Integer = 0 'The number of records that have been printed in the current page
'Used to determine whether to change the page DIM STRUPDATA AS STRING = "" "The previous data of the current data DIM STRNONCE AS STRING =" "current data
While Printingline DataGridRow = DataGridTable.Rows (PrintRecordComplete) 'record to determine the current line number to be printed 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 'The data paging IF STRUPDATA <> "" IF STRUPDATA <> "" IF STRUPDATA <> "" IF STRUPDATA <> "IF STRUPDATA <> DataGridrow (ColumnText (0)) THEN EV.hasMorePages = true exit sub END if End if endiff Ev.graphics.drawString (ColumnText, Drawbrush, DrawPoint) Strupdata = DATAGRIDROW (ColumnText (0)) The first column of the record will be saved after the current row data print is completed, (or not This statement is only to clear) Next DrawPoint.x = x_unit * 1 drawpoint.y = y_Unit * (PrintingLine 1 2) Call Drawline (DrawPoint, EV) PrintingLine = 1 PrintRecordcomplete = 1 'After printing the last record, the print is over.' Such as: Currently 500 records. From 0, print, actually print the first record. You can print 500, the actual record is the 501th record. That is the last one 'DataGridTable.Rows.count is the number of actual records in the table, how many records (starting from 1) Article 501, out of range, then ends. if PrintRecordComplete> = DataGridTable.Rows.Count then ev.HasMorePages = False Exit Sub end if end WhilePrintingPageNumber = 1 if PrintingPageNumber> PageNumber then ev.HasMorePages = False Else ev.hasmorepages = true exit while endiff End while End Sub 'Picture line only, you will specify the start position of the current row, X, Y is the print position of the current line text 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 asprintdatagrid.printdataGrid AA = New PrintDataGrid.printDataGrid (DataGrid1) Aa.SetPrintFont () = new system.drawing.font ("Song", 12) 'printing text fonts aa.setprinTrecordNumber = 30' The number of records to print per page aa.print () Function: You can directly print the data in the DataGrid control, but you cannot format the data, the function is still perfect :) I hope everyone will advise. Thank you.