Printing Datagrid

Jackson1998

Member
Joined
Jun 1, 2014
Messages
10
Programming Experience
1-3
Hello guys , i'm new to here
i was trying to print all data in Datagridview , i searched a lot and i came across code below , i just don't know what coder means by queryform , any help will be thankful

'在按钮事件里调用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

 
It should be fairly obvious that 'queryform' is a form. Presumably it's a form that executes a query and displays the result set in a DataGrid. Of course, a DataGrid and a DataGridView are two different things, hence the different names. I suggest that you look here:

DataGridPrinter - A class to print data grid in a nicely formatted way

That thread was created to provide a class to print a DataGrid but a later post provides a similar class to print a DataGridView.
 
Back
Top