Imports System.Data.SqlClient
Imports System.Collections.Generic
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim connectionString As String = "data source=SQLst; database=MVar2; User ID=Wep; Password=201ive; Integrated Security=false;"
Dim sql As String = "SELECT * FROM Priailer"
Dim connection As New SqlConnection(connectionString)
Dim dataadapter As New SqlDataAdapter(sql, connection)
Dim ds As New DataSet()
connection.Open()
dataadapter.Fill(ds, "Authors_table")
connection.Close()
DataGridView1.DataSource = ds
DataGridView1.DataMember = "Authors_table"
DataGridView1.CurrentCell.Style.BackColor = Color.Red
For m = 0 To 5
For d = 0 To 3
DataGridView1.Item(d, m).Style.BackColor = Color.Red
Next d
Next m
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Call PrintDGV.Print_DataGridView(DataGridView1)
End Sub
End Class
' Grid Object Printing - This routine was retrieved from an internet web-site (I don't remember which)
Public Class PrintDGV
Private Shared UseOldModel As Boolean = False
Private Shared StrFormat As StringFormat ' Holds content of a TextBox Cell to write by DrawString
Private Shared StrFormatComboBox As StringFormat ' Holds content of a Boolean Cell to write by DrawImage
Private Shared CellButton As Button ' Holds the Contents of Button Cell
Private Shared CellCheckBox As CheckBox ' Holds the Contents of CheckBox Cell
Private Shared CellComboBox As ComboBox ' Holds the Contents of ComboBox Cell
Private Shared TotalWidth As Int16 ' Summation of Columns widths
Private Shared RowPos As Int16 ' Position of currently printing row
Private Shared NewPage As Boolean ' Indicates if a new page reached
Private Shared PageNo As Int16 ' Number of pages to print
Private Shared ColumnLefts As New ArrayList ' Left Coordinate of Columns
Private Shared ColumnWidths As New ArrayList ' Width of Columns
Private Shared ColumnTypes As New ArrayList ' DataType of Columns
Private Shared CellHeight As Int16 ' Height of DataGrid Cell
Private Shared RowsPerPage As Int16 ' Number of Rows per Page
Private Shared WithEvents PrintDoc As New System.Drawing.Printing.PrintDocument ' PrintDocumnet Object used for printing
Private Shared PrintTitle As String = "" ' Header of pages
Private Shared dgv As DataGridView ' Holds DataGrid Object to print its contents
Private Shared SelectedColumns As New List(Of String) ' The Columns Selected by user to print.
Private Shared AvailableColumns As New List(Of String) ' All Columns avaiable in DataGrid
Private Shared PrintAllRows As Boolean = True ' True = print all rows, False = print selected rows
Private Shared FitToPageWidth As Boolean = True ' True = Fits selected columns to page width , False = Print columns as showed
Private Shared HeaderHeight As Int16 = 0
Private Shared ColPages As Integer = 0, ColCount() As Integer, ColPrint(,) As Integer, ColumnLeft(,) As Integer, ColumnType(,) As System.Type, ColumnWidth(,) As Integer, HeaderCol As Integer = -1
Private Shared ColPage As Integer = 0
Private Shared ColPageLimit As Integer = 100
Public Shared Sub Print_DataGridView(ByVal dgv1 As DataGridView)
Dim ppvw As PrintPreviewDialog
Try
' Getting DataGridView object to print
dgv = dgv1
ColPageLimit = dgv1.ColumnCount
' Getting all Coulmns Names in the DataGridView
AvailableColumns.Clear()
For Each c As DataGridViewColumn In dgv.Columns
If Not c.Visible Then Continue For
AvailableColumns.Add(c.HeaderText)
Next
' Showing the PrintOption Form
Dim dlg As New PrintOptions(AvailableColumns)
If dlg.ShowDialog() <> DialogResult.OK Then Exit Sub
' Saving some printing attributes
PrintTitle = dlg.PrintTitle
PrintAllRows = dlg.PrintAllRows
FitToPageWidth = dlg.FitToPageWidth
SelectedColumns = dlg.GetSelectedColumns
RowsPerPage = 0
PrintDoc.DefaultPageSettings.Landscape = True
PrintDoc.DefaultPageSettings.Margins.Left = 10
PrintDoc.DefaultPageSettings.Margins.Right = 10
ppvw = New PrintPreviewDialog
ppvw.Document = PrintDoc
' Showing the Print Preview Page
ppvw.Width = dgv1.Width
ppvw.Height = dgv1.Height
If ppvw.Width > Screen.PrimaryScreen.WorkingArea.Width Then _
ppvw.Width = Screen.PrimaryScreen.WorkingArea.Width
If ppvw.Height > Screen.PrimaryScreen.WorkingArea.Height Then _
ppvw.Height = Screen.PrimaryScreen.WorkingArea.Height
If ppvw.ShowDialog() <> DialogResult.OK Then Exit Sub
' Printing the Documnet
PrintDoc.Print()
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
End Try
End Sub
Private Shared Sub PrintDoc_BeginPrint(ByVal sender As Object, _
ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDoc.BeginPrint
Try
' Formatting the Content of Text Cells to print
StrFormat = New StringFormat
StrFormat.Alignment = StringAlignment.Near
StrFormat.LineAlignment = StringAlignment.Center
StrFormat.Trimming = StringTrimming.EllipsisCharacter
' Formatting the Content of Combo Cells to print
StrFormatComboBox = New StringFormat
StrFormatComboBox.LineAlignment = StringAlignment.Center
StrFormatComboBox.FormatFlags = StringFormatFlags.NoWrap
StrFormatComboBox.Trimming = StringTrimming.EllipsisCharacter
ColumnLefts.Clear()
ColumnWidths.Clear()
ColumnTypes.Clear()
CellHeight = 0
RowsPerPage = 0
' For various column types
CellButton = New Button
CellCheckBox = New CheckBox
CellComboBox = New ComboBox
TotalWidth = 0
For Each GridCol As DataGridViewColumn In dgv.Columns
If Not GridCol.Visible Then Continue For
If Not SelectedColumns.Contains(GridCol.HeaderText) Then Continue For
TotalWidth += GridCol.Width
Next
ColPages = 0
Dim TheCols As Integer = 0
ReDim ColCount(ColPages)
ColCount(ColPages) = 0
ReDim ColPrint(ColPageLimit, TheCols), ColumnLeft(ColPageLimit, TheCols), ColumnType(ColPageLimit, TheCols), ColumnWidth(ColPageLimit, TheCols)
HeaderCol = -1
Dim TmpWidth As Integer = 0, TmpLeft As Integer = 0
For Each GridCol As DataGridViewColumn In dgv.Columns
If ColPages >= ColPageLimit Then
Exit For
End If
If Not GridCol.Visible Then Continue For
If Not SelectedColumns.Contains(GridCol.HeaderText) Then Continue For
If HeaderCol = -1 Then _
HeaderCol = GridCol.Index
If FitToPageWidth Then
TmpWidth = CType(Math.Floor(GridCol.Width / TotalWidth * _
TotalWidth * (PrintDoc.DefaultPageSettings.PrintableArea.Height / TotalWidth)), Int16)
TmpLeft += TmpWidth
Else
TmpWidth = GridCol.Width
If TmpLeft + TmpWidth > PrintDoc.DefaultPageSettings.PrintableArea.Height Then
ColPages += 1
ReDim Preserve ColCount(ColPages)
ColCount(ColPages) = 0
ReDim Preserve ColPrint(ColPageLimit, TheCols), ColumnLeft(ColPageLimit, TheCols), ColumnType(ColPageLimit, TheCols), ColumnWidth(ColPageLimit, TheCols)
If HeaderCol > -1 Then
Dim GC As DataGridViewColumn = dgv.Columns(HeaderCol)
ColumnLeft(ColPages, ColCount(ColPages)) = PrintDoc.DefaultPageSettings.Margins.Left
ColumnWidth(ColPages, ColCount(ColPages)) = GC.Width
ColPrint(ColPages, ColCount(ColPages)) = HeaderCol
ColumnType(ColPages, ColCount(ColPages)) = GC.GetType
ColCount(ColPages) += 1
TmpLeft = GC.Width
Else
TmpLeft = 0
End If
End If
TmpLeft += TmpWidth
End If
If TheCols < ColCount(ColPages) Then
TheCols = ColCount(ColPages)
ReDim Preserve ColPrint(ColPageLimit, TheCols), ColumnLeft(ColPageLimit, TheCols), ColumnType(ColPageLimit, TheCols), ColumnWidth(ColPageLimit, TheCols)
End If
ColumnWidth(ColPages, ColCount(ColPages)) = TmpWidth
ColumnLeft(ColPages, ColCount(ColPages)) = TmpLeft - TmpWidth + PrintDoc.DefaultPageSettings.Margins.Left
ColumnType(ColPages, ColCount(ColPages)) = GridCol.GetType
ColPrint(ColPages, ColCount(ColPages)) = GridCol.Index
ColCount(ColPages) += 1
Next
ColPage = 0
PageNo = 1
NewPage = True
RowPos = 0
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
End Try
End Sub
Private Shared Sub PrintPage(ByVal e As System.Drawing.Printing.PrintPageEventArgs)
Dim tmpTop As Int16 = e.MarginBounds.Top
Dim ColAt As Integer = 0, PageAt As Integer = 0
Try
If PageNo = 1 Then
For PageAt = 0 To ColPages - 1
For ColAt = 0 To ColCount(PageAt) - 1
Dim GridCol As DataGridViewColumn = dgv.Columns(ColPrint(PageAt, ColAt))
HeaderHeight = e.Graphics.MeasureString(GridCol.HeaderText, _
GridCol.InheritedStyle.Font, ColumnWidth(PageAt, ColAt)).Height + 11
Next
Next
End If
Do While RowPos <= dgv.Rows.Count - 1
Dim GridRow As DataGridViewRow = dgv.Rows(RowPos)
If GridRow.IsNewRow OrElse (Not PrintAllRows AndAlso Not GridRow.Selected) Then
RowPos += 1 : Continue Do
End If
CellHeight = GridRow.Height
If tmpTop + CellHeight >= e.MarginBounds.Height + e.MarginBounds.Top Then
NewPage = True
PageNo += 1
e.HasMorePages = True
Exit Sub
Else
If NewPage Then
' Draw Header
e.Graphics.DrawString(PrintTitle, New Font(dgv.Font, FontStyle.Bold), _
Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top - _
e.Graphics.MeasureString(PrintTitle, New Font(dgv.Font, _
FontStyle.Bold), e.MarginBounds.Width).Height - 13)
Dim s As String = Now.ToLongDateString + " " + Now.ToShortTimeString
e.Graphics.DrawString(s, New Font(dgv.Font, FontStyle.Bold), _
Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - _
e.Graphics.MeasureString(s, New Font(dgv.Font, FontStyle.Bold), _
e.MarginBounds.Width).Width), e.MarginBounds.Top - _
e.Graphics.MeasureString(PrintTitle, _
New Font(New Font(dgv.Font, FontStyle.Bold), FontStyle.Bold), _
e.MarginBounds.Width).Height - 13)
' Draw Columns
tmpTop = e.MarginBounds.Top
For ColAt = 0 To ColCount(ColPage) - 1
e.Graphics.FillRectangle(New SolidBrush(Drawing.Color.LightGray), _
New Rectangle(ColumnLeft(ColPage, ColAt), tmpTop, ColumnWidth(ColPage, ColAt), HeaderHeight))
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(ColumnLeft(ColPage, ColAt), _
tmpTop, ColumnWidth(ColPage, ColAt), HeaderHeight))
Dim GridCol As DataGridViewColumn = dgv.Columns(ColPrint(ColPage, ColAt))
e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font, _
New SolidBrush(GridCol.InheritedStyle.ForeColor), _
New RectangleF(ColumnLeft(ColPage, ColAt), tmpTop, ColumnWidth(ColPage, ColAt), _
HeaderHeight), StrFormat)
Next
NewPage = False
tmpTop += HeaderHeight
End If
For ColAt = 0 To ColCount(ColPage) - 1
Dim Cel As DataGridViewCell = GridRow.Cells(ColPrint(ColPage, ColAt))
If Cel.Value Is Nothing Then _
Cel.Value = ""
If ColumnType(ColPage, ColAt) Is GetType(DataGridViewTextBoxColumn) OrElse ColumnType(ColPage, ColAt) Is GetType(DataGridViewLinkColumn) Then
e.Graphics.DrawString(Cel.Value.ToString, Cel.InheritedStyle.Font, _
New SolidBrush(Cel.InheritedStyle.ForeColor), _
New RectangleF(ColumnLeft(ColPage, ColAt), tmpTop, ColumnWidth(ColPage, ColAt), _
CellHeight), StrFormat)
' For the Button Column
ElseIf ColumnType(ColPage, ColAt) Is GetType(DataGridViewButtonColumn) Then
CellButton.Text = Cel.Value.ToString
CellButton.Size = New Size(ColumnWidth(ColPage, ColAt), CellHeight)
Dim bmp As New Bitmap(CellButton.Width, CellButton.Height)
CellButton.DrawToBitmap(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height))
e.Graphics.DrawImage(bmp, New Point(ColumnLeft(ColPage, ColAt), tmpTop))
' For the CheckBox Column
ElseIf ColumnType(ColPage, ColAt) Is GetType(DataGridViewCheckBoxColumn) Then
CellCheckBox.Size = New Size(14, 14)
CellCheckBox.Checked = CType(Cel.Value, Boolean)
Dim bmp As New Bitmap(ColumnWidth(ColPage, ColAt), CellHeight)
Dim tmpGraphics As Graphics = Graphics.FromImage(bmp)
tmpGraphics.FillRectangle(Brushes.White, New Rectangle(0, 0, bmp.Width, bmp.Height))
CellCheckBox.DrawToBitmap(bmp, New Rectangle(CType((bmp.Width - _
CellCheckBox.Width) / 2, Int32), CType((bmp.Height - _
CellCheckBox.Height) / 2, Int32), CellCheckBox.Width, _
CellCheckBox.Height))
e.Graphics.DrawImage(bmp, New Point(ColumnLeft(ColPage, ColAt), tmpTop))
' For the ComboBox Column
ElseIf ColumnType(ColPage, ColAt) Is GetType(DataGridViewComboBoxColumn) Then
CellComboBox.Size = New Size(ColumnWidth(ColPage, ColAt), CellHeight)
Dim bmp As New Bitmap(CellComboBox.Width, CellComboBox.Height)
CellComboBox.DrawToBitmap(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height))
e.Graphics.DrawImage(bmp, New Point(ColumnLeft(ColPage, ColAt), tmpTop))
e.Graphics.DrawString(Cel.Value.ToString, Cel.InheritedStyle.Font, _
New SolidBrush(Cel.InheritedStyle.ForeColor), _
New RectangleF(ColumnLeft(ColPage, ColAt) + 1, tmpTop, ColumnWidth(ColPage, ColAt) _
- 16, CellHeight), StrFormatComboBox)
' For the Image Column
ElseIf ColumnType(ColPage, ColAt) Is GetType(DataGridViewImageColumn) Then
Dim CelSize As Rectangle = New Rectangle(ColumnLeft(ColPage, ColAt), tmpTop, ColumnWidth(ColPage, ColAt), CellHeight)
Dim ImgSize As Size = CType(Cel.FormattedValue, Image).Size
e.Graphics.DrawImage(Cel.FormattedValue, New Rectangle(ColumnLeft(ColPage, ColAt) _
+ CType(((CelSize.Width - ImgSize.Width) / 2), Int32), _
tmpTop + CType(((CelSize.Height - ImgSize.Height) / 2), _
Int32), CType(Cel.FormattedValue, Image).Width, CType(Cel.FormattedValue, _
Image).Height))
End If
' Drawing Cells Borders
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(ColumnLeft(ColPage, ColAt), tmpTop, ColumnWidth(ColPage, ColAt), CellHeight))
Next
tmpTop += CellHeight
End If
RowPos += 1
Loop
ColPage += 1
If ColPage <= ColPages Then
e.HasMorePages = True
NewPage = True
RowPos = 0
Else
e.HasMorePages = False
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
End Try
End Sub
Private Shared Sub PrintOldPage(ByVal e As System.Drawing.Printing.PrintPageEventArgs)
Dim tmpWidth As Int16, i As Int16
Dim tmpTop As Int16 = e.MarginBounds.Top
Dim tmpLeft As Int16 = e.MarginBounds.Left
Try
' Before starting first page, it saves Width & Height of Headers and CoulmnType
If PageNo = 1 Then
For Each GridCol As DataGridViewColumn In dgv.Columns
If Not GridCol.Visible Then Continue For
If Not SelectedColumns.Contains(GridCol.HeaderText) Then Continue For
' Detemining whether the columns are fitted to page or not.
If FitToPageWidth Then
tmpWidth = CType(Math.Floor(GridCol.Width / TotalWidth * _
TotalWidth * (e.MarginBounds.Width / TotalWidth)), Int16)
Else
tmpWidth = GridCol.Width
End If
HeaderHeight = e.Graphics.MeasureString(GridCol.HeaderText, _
GridCol.InheritedStyle.Font, tmpWidth).Height + 11
ColumnLefts.Add(tmpLeft)
ColumnWidths.Add(tmpWidth)
ColumnTypes.Add(GridCol.GetType)
tmpLeft += tmpWidth
Next
End If
' Printing Current Page, Row by Row
RowPos = 0
Do While RowPos <= dgv.Rows.Count - 1
Dim GridRow As DataGridViewRow = dgv.Rows(RowPos)
If GridRow.IsNewRow OrElse (Not PrintAllRows AndAlso Not GridRow.Selected) Then
RowPos += 1 : Continue Do
End If
CellHeight = GridRow.Height
If tmpTop + CellHeight >= e.MarginBounds.Height + e.MarginBounds.Top Then
DrawFooter(e, RowsPerPage)
NewPage = True
PageNo += 1
e.HasMorePages = True
Exit Sub
Else
If NewPage Then
' Draw Header
e.Graphics.DrawString(PrintTitle, New Font(dgv.Font, FontStyle.Bold), _
Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top - _
e.Graphics.MeasureString(PrintTitle, New Font(dgv.Font, _
FontStyle.Bold), e.MarginBounds.Width).Height - 13)
Dim s As String = Now.ToLongDateString + " " + Now.ToShortTimeString
e.Graphics.DrawString(s, New Font(dgv.Font, FontStyle.Bold), _
Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - _
e.Graphics.MeasureString(s, New Font(dgv.Font, FontStyle.Bold), _
e.MarginBounds.Width).Width), e.MarginBounds.Top - _
e.Graphics.MeasureString(PrintTitle, _
New Font(New Font(dgv.Font, FontStyle.Bold), FontStyle.Bold), _
e.MarginBounds.Width).Height - 13)
' Draw Columns
tmpTop = e.MarginBounds.Top
i = 0
For Each GridCol As DataGridViewColumn In dgv.Columns
If Not GridCol.Visible Then Continue For
If Not SelectedColumns.Contains(GridCol.HeaderText) Then Continue For
e.Graphics.FillRectangle(New SolidBrush(Drawing.Color.LightGray), _
New Rectangle(ColumnLefts(i), tmpTop, ColumnWidths(i), HeaderHeight))
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(ColumnLefts(i), _
tmpTop, ColumnWidths(i), HeaderHeight))
e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font, _
New SolidBrush(GridCol.InheritedStyle.ForeColor), _
New RectangleF(ColumnLefts(i), tmpTop, ColumnWidths(i), _
HeaderHeight), StrFormat)
i += 1
Next
NewPage = False
tmpTop += HeaderHeight
End If
i = 0
For Each Cel As DataGridViewCell In GridRow.Cells
If Not Cel.OwningColumn.Visible Then Continue For
If Cel.Value Is Nothing Then
Cel.Value = ""
End If
If Not SelectedColumns.Contains(Cel.OwningColumn.HeaderText) Then
Continue For
End If
' For the TextBox Column
If ColumnTypes(i) Is GetType(DataGridViewTextBoxColumn) OrElse _
ColumnTypes(i) Is GetType(DataGridViewLinkColumn) Then
e.Graphics.DrawString(Cel.Value.ToString, Cel.InheritedStyle.Font, _
New SolidBrush(Cel.InheritedStyle.ForeColor), _
New RectangleF(ColumnLefts(i), tmpTop, ColumnWidths(i), _
CellHeight), StrFormat)
' For the Button Column
ElseIf ColumnTypes(i) Is GetType(DataGridViewButtonColumn) Then
CellButton.Text = Cel.Value.ToString
CellButton.Size = New Size(ColumnWidths(i), CellHeight)
Dim bmp As New Bitmap(CellButton.Width, CellButton.Height)
CellButton.DrawToBitmap(bmp, New Rectangle(0, 0, _
bmp.Width, bmp.Height))
e.Graphics.DrawImage(bmp, New Point(ColumnLefts(i), tmpTop))
' For the CheckBox Column
ElseIf ColumnTypes(i) Is GetType(DataGridViewCheckBoxColumn) Then
CellCheckBox.Size = New Size(14, 14)
CellCheckBox.Checked = CType(Cel.Value, Boolean)
Dim bmp As New Bitmap(ColumnWidths(i), CellHeight)
Dim tmpGraphics As Graphics = Graphics.FromImage(bmp)
tmpGraphics.FillRectangle(Brushes.White, New Rectangle(0, 0, _
bmp.Width, bmp.Height))
CellCheckBox.DrawToBitmap(bmp, New Rectangle(CType((bmp.Width - _
CellCheckBox.Width) / 2, Int32), CType((bmp.Height - _
CellCheckBox.Height) / 2, Int32), CellCheckBox.Width, _
CellCheckBox.Height))
e.Graphics.DrawImage(bmp, New Point(ColumnLefts(i), tmpTop))
' For the ComboBox Column
ElseIf ColumnTypes(i) Is GetType(DataGridViewComboBoxColumn) Then
CellComboBox.Size = New Size(ColumnWidths(i), CellHeight)
Dim bmp As New Bitmap(CellComboBox.Width, CellComboBox.Height)
CellComboBox.DrawToBitmap(bmp, New Rectangle(0, 0, _
bmp.Width, bmp.Height))
e.Graphics.DrawImage(bmp, New Point(ColumnLefts(i), tmpTop))
e.Graphics.DrawString(Cel.Value.ToString, Cel.InheritedStyle.Font, _
New SolidBrush(Cel.InheritedStyle.ForeColor), _
New RectangleF(ColumnLefts(i) + 1, tmpTop, ColumnWidths(i) _
- 16, CellHeight), StrFormatComboBox)
' For the Image Column
ElseIf ColumnTypes(i) Is GetType(DataGridViewImageColumn) Then
Dim CelSize As Rectangle = New Rectangle(ColumnLefts(i), _
tmpTop, ColumnWidths(i), CellHeight)
Dim ImgSize As Size = CType(Cel.FormattedValue, Image).Size
e.Graphics.DrawImage(Cel.FormattedValue, New Rectangle(ColumnLefts(i) _
+ CType(((CelSize.Width - ImgSize.Width) / 2), Int32), _
tmpTop + CType(((CelSize.Height - ImgSize.Height) / 2), _
Int32), CType(Cel.FormattedValue, Image).Width, CType(Cel.FormattedValue, _
Image).Height))
End If
' Drawing Cells Borders
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(ColumnLefts(i), _
tmpTop, ColumnWidths(i), CellHeight))
i += 1
Next
tmpTop += CellHeight
End If
RowPos += 1
' For the first page it calculates Rows per Page
If PageNo = 1 Then
RowsPerPage += 1
End If
Loop
If RowsPerPage = 0 Then Exit Sub
' Write Footer (Page Number)
DrawFooter(e, RowsPerPage)
e.HasMorePages = False
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
End Try
End Sub
Private Shared Sub PrintDoc_PrintPage(ByVal sender As Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDoc.PrintPage
If UseOldModel Then
PrintOldPage(e)
Else
PrintPage(e)
End If
End Sub
Private Shared Sub DrawFooter(ByVal e As System.Drawing.Printing.PrintPageEventArgs, ByVal RowsPerPage As Int32)
Dim cnt As Integer
' Detemining rows number to print
If PrintAllRows Then
If dgv.Rows(dgv.Rows.Count - 1).IsNewRow Then
' When the DataGridView doesn't allow adding rows
cnt = dgv.Rows.Count - 2
Else
' When the DataGridView allows adding rows
cnt = dgv.Rows.Count - 1
End If
Else
cnt = dgv.SelectedRows.Count
End If
' Writing the Page Number on the Bottom of Page
Dim PageNum As String = PageNo.ToString + " of " + _
Math.Ceiling(cnt / RowsPerPage).ToString
e.Graphics.DrawString(PageNum, dgv.Font, Brushes.Black, _
e.MarginBounds.Left + (e.MarginBounds.Width - _
e.Graphics.MeasureString(PageNum, dgv.Font, _
e.MarginBounds.Width).Width) / 2, e.MarginBounds.Top + _
e.MarginBounds.Height + 31)
End Sub
End Class