Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- Dim MyAlpha As Integer
- Dim MyRed As Integer
- Dim MyGreen As Integer
- Dim MyBlue As Integer
- 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
- ' Initialize the random-number generator.
- Randomize()
- ' Generate random value between 1 and 6.
- MyAlpha = CInt(Int((254 * Rnd()) + 0))
- ' Initialize the random-number generator.
- Randomize()
- ' Generate random value between 1 and 6.
- MyRed = CInt(Int((254 * Rnd()) + 0))
- ' Initialize the random-number generator.
- Randomize()
- ' Generate random value between 1 and 6.
- MyGreen = CInt(Int((254 * Rnd()) + 0))
- ' Initialize the random-number generator.
- Randomize()
- ' Generate random value between 1 and 6.
- MyBlue = CInt(Int((254 * Rnd()) + 0))
- Dim penColor As New Pen(Color.FromArgb(MyAlpha, MyRed, MyGreen, MyBlue))
- ' Drawing Cells Borders
- e.Graphics.DrawRectangle(penColor, 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement