Print Datagridview Class
Print Datagridview Class
http://pundtnotes.blogspot.in/2008/12/vb-net-printing-print-datagrid-view.html
objTest = Nothing
**************
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Drawing.Printing
Imports System.Drawing
Private m_status As String = "" ' If spanning multi pages mark m_status.
' Current status codes used while printing
' "" =[Blank] means 1st page start
' "1" = any numeric is a row count of the next fiscal impact row number that needs
to print.
' "EL" = "Closing FI table line"
' "TL" = "Totals line"
' "PARA" = "Paragraphs" - This is the State Effect and Comments Blob of text.
Needed special treatment.
' "RD" = "Print out on next page beginning with Rounding (Entire Row including
following row)"
' "SB" = "Print out on next page beginning with SmallBusinessEffect (Entire Row
including following row)"
MarginTop = asMarginTop
MarginBottom = asMarginBottom
MarginLeft = asMarginLeft
MarginRight = asMarginRight
If asOrientation = Orientation.Landscape Then
Landscape = True
Else
Landscape = False
End If
FontName = asFontName
End Sub
End Structure
Try
m_intPGNumber = 0
m_PrintDocument1 = New System.Drawing.Printing.PrintDocument
' Starting point of the print.
Dim PSize As Integer = 10
'Dim PHi As Double
With m_PrintDocument1.DefaultPageSettings
' If we needed a custom paper size this is how we would do it.
'Dim Ps As PaperSize
'PHi = PSize * 20 + 350
'Ps = New PaperSize("Cust", 800, PHi)
'.PaperSize = Ps
.Margins.Left = objPageInfo.MarginLeft
.Margins.Right = objPageInfo.MarginRight
.Margins.Top = objPageInfo.MarginTop
.Margins.Bottom = objPageInfo.MarginBottom
.Landscape = objPageInfo.Landscape
End With
' Create some standardized fonts that can be used in this code.
m_fntHead = New Font(objPageInfo.FontName, 12, FontStyle.Bold)
m_fntTable = New Font(objPageInfo.FontName, 8)
m_fntTableBold = New Font(objPageInfo.FontName, 8, FontStyle.Bold)
m_fntTitle = New Font(objPageInfo.FontName, 10, FontStyle.Bold)
m_fntRegular = New Font(objPageInfo.FontName, 10, FontStyle.Regular)
m_axisX1 = m_PrintDocument1.DefaultPageSettings.Margins.Left
With m_PrintDocument1.DefaultPageSettings
m_intPageWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
End With
m_status = ""
' The line below triggers the m_PrintDocument1_PrintPage event until .Haspages =
False
If Destination = PrintDestination.Printer Then
' No print preview, just send job to the printer.
m_PrintDocument1.Print()
Else
' See job in a print preview window prior to sending job to the printer.
dlgSwitchPrinter.Document = m_PrintDocument1
dlgSwitchPrinter.PrinterSettings = m_PrintDocument1.PrinterSettings
If dlgSwitchPrinter.ShowDialog() <> Windows.Forms.DialogResult.OK Then
Exit Try
End If
dlgPrintPreview.Document = m_PrintDocument1
' Preview.
dlgPrintPreview.WindowState = Windows.Forms.FormWindowState.Maximized
dlgPrintPreview.ShowDialog()
End If
Catch ex As Exception
Throw ex
Finally
m_PrintDocument1.Dispose() : m_PrintDocument1 = Nothing
dlgPrintPreview.Dispose() : dlgPrintPreview = Nothing
dlgSwitchPrinter.Dispose() : dlgSwitchPrinter = Nothing
End Try
End Sub
'Dim rectCurrent As RectangleF ' Used to write text in the FI table region.
Dim layout_rect As RectangleF ' Used to flow text in State Effect & Comments
regions.
Dim strOutput As String = ""
Dim intColumnCnt As Integer = 1
Dim intRowCNT As Integer = 0
Dim intColCNT As Integer = 0
Dim text_size As System.Drawing.SizeF
Dim characters_fitted As Integer = 0
Dim lines_filled As Integer = 0
Dim intMaxRowHeight As Integer = 0
Dim string_format As New StringFormat
'Dim fntDynamic As Font ' Used to store the font for each paragraph.
Dim bolOKToContinue As Boolean = True
Dim bolPrintColumnHeaders As Boolean = True
'Dim myBrush As SolidBrush
Dim oBitmap As Bitmap
Try
m_intPGNumber += 1
m_axisY = m_PrintDocument1.DefaultPageSettings.Margins.Top + CInt(m_fntHead.Height)
' Put the pen at position (0,0) in the writable area (inside the margins)
' If the layout rectangle's height < height =" 1" bolprintcolumnheaders =" True"
visible =" True" alignment =" StringAlignment.Center" formatflags ="
StringFormatFlags.LineLimit" trimming =" StringTrimming.Word" layout_rect =" New"
intcolumncnt =" m_axisXCol.Count" introwcnt =" 0" intcolcnt =" 1" intmaxrowheight
=" 0" isnewrow =" True" hasmorepages =" False" introwcnt =" CType(m_status,"
boloktocontinue =" True" m_status = "" boloktocontinue =" False" m_status = ""
boloktocontinue =" True" boloktocontinue =" True" introwcnt =" 57" m_status ="
m_status" visible =" True" m_status =" intRowCNT.ToString" hasmorepages =" True"
alignment =" StringAlignment.Center" alignment =" StringAlignment.Near" alignment
=" StringAlignment.Far" alignment =" StringAlignment.Near" formatflags ="
StringFormatFlags.LineLimit" trimming =" StringTrimming.Word" layout_rect =" New">
"Color [Empty]" Then
' e.Graphics.FillRectangle(New
SolidBrush(row.DataGridView.DefaultCellStyle.BackColor), layout_rect)
'End If
'End If
Case "System.Windows.Forms.DataGridViewCheckBoxCell"
oCheckbox = New System.Windows.Forms.CheckBox
oCheckbox.Size = New Size(14, 14)
oCheckbox.Checked = CType(cell.Value, Boolean) '
'Dim oBitmap As New Bitmap(CType(layout_rect.Width, Int16),
CType(layout_rect.Height, Int16)) 'nHeight
oBitmap = New Bitmap(CType(layout_rect.Width, Int16), CType(layout_rect.Height,
Int16)) 'nHeight
'Dim oTempGraphics As Graphics = Graphics.FromImage(oBitmap)
If row.DefaultCellStyle.BackColor <> Color.Yellow Then
'oTempGraphics.FillRectangle(Brushes.White, New Rectangle(0, 0, oBitmap.Width,
oBitmap.Height))
End If
oCheckbox.DrawToBitmap(oBitmap, New Rectangle(CType((oBitmap.Width -
oCheckbox.Width) / 2, Int32), CType((oBitmap.Height - oCheckbox.Height) / 2,
Int32), oCheckbox.Width, oCheckbox.Height))
'e.Graphics.DrawImage(oBitmap, New Point(m_axisXCol.Item(intColCNT), m_axisY - 5))
e.Graphics.DrawImage(oBitmap, m_axisXCol.Item(intColCNT), m_axisY,
layout_rect.Width, layout_rect.Height - 5)
oBitmap.Dispose()
'oTempGraphics.Dispose()
Case "System.Windows.Forms.DataGridViewComboBoxCell"
oComboBox = New System.Windows.Forms.ComboBox
oComboBox.Size = New Size(layout_rect.Width, layout_rect.Height)
'Dim oBitmap As New Bitmap(oComboBox.Width, CType(row.Height, System.Int16))
oBitmap = New Bitmap(oComboBox.Width, CType(row.Height, System.Int16))
oComboBox.DrawToBitmap(oBitmap, New Rectangle(0, CType((row.Height -
oComboBox.Height) / 2, System.Int16), oBitmap.Width, oBitmap.Height))
'oComboBox.DrawToBitmap(oBitmap, New Rectangle(layout_rect.X, layout_rect.Y,
layout_rect.Width, layout_rect.Height))
e.Graphics.DrawImage(oBitmap, m_axisXCol.Item(intColCNT), m_axisY,
layout_rect.Width, layout_rect.Height - 5)
text_size = e.Graphics.MeasureString( _
cell.Value, m_fntTable, _
New SizeF(layout_rect.Width, layout_rect.Height), _
string_format, characters_fitted, lines_filled)
oBitmap.Dispose()
Case Else
' ???
' Try text
text_size = e.Graphics.MeasureString( _
cell.Value, m_fntTable, _
New SizeF(layout_rect.Width, layout_rect.Height), _
string_format, characters_fitted, lines_filled)
e.Graphics.DrawString(cell.Value.ToString, _
m_fntTable, New SolidBrush(cell.InheritedStyle.ForeColor), _
layout_rect, string_format)
End Select
'text_size = e.Graphics.MeasureString( _
' cell.Value, m_fntTable, _
' New SizeF(layout_rect.Width, layout_rect.Height), _
' string_format, characters_fitted, lines_filled)
'e.Graphics.DrawString(cell.Value.ToString, _
' m_fntTable, Brushes.Black, _
' layout_rect, string_format)
Catch ex As Exception
Throw ex
Finally
e.Graphics.DrawString(m_strRunDateAndTime & " - page " & m_intPGNumber.ToString, _
m_fntTable, Brushes.Black, m_axisX1, e.MarginBounds.Top) 'm_axisY)
End Try
End Sub
Catch ex As Exception
Throw ex
End Try
End Function
Catch ex As Exception
Throw ex
End Try
End Sub
FindCellAttributes = Nothing
If objCell.Style.Font IsNot Nothing Then
objCellAttributes.FontName = objCell.Style.Font.Name
objCellAttributes.FontSize = objCell.Style.Font.Size
Else
If m_dgvToPrint.Rows(objCell.RowIndex).DefaultCellStyle.Font IsNot Nothing Then
objCellAttributes.FontName =
m_dgvToPrint.Rows(objCell.RowIndex).DefaultCellStyle.Font.Name
objCellAttributes.FontSize =
m_dgvToPrint.Rows(objCell.RowIndex).DefaultCellStyle.Font.Size
Else
objCellAttributes.FontName = m_dgvToPrint.Font.Name
objCellAttributes.FontSize = m_dgvToPrint.Font.Size
End If
End If
'objCellAttributes.ForeColor =
ConvertSystemColorToBrush(m_dgvToPrint.Rows(objCell.RowIndex).DefaultCellStyle.Fore
Color)
'objCellAttributes.BackColor =
ConvertSystemColorToBrush(m_dgvToPrint.Rows(objCell.RowIndex).DefaultCellStyle.Back
Color)
'objCellAttributes.ForeColor = ConvertSystemColorToBrush(objCell.Style.ForeColor)
'objCellAttributes.BackColor = ConvertSystemColorToBrush(objCell.Style.BackColor)
Catch ex As Exception
objCellAttributes = Nothing
Throw ex
End Try
End Function
End Class