Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As _
System.Drawing.Printing.PrintPageEventArgs) Handles _
PrintDocument1.PrintPage
Static oColumnLefts As New ArrayList
Static oColumnWidths As New ArrayList
Static oColumnTypes As New ArrayList
Static nHeight As Int16
Dim nWidth, i, nRowsPerPage As Int16
Dim nTop As Int16 = e.MarginBounds.Top
Dim nLeft As Int16 = e.MarginBounds.Left
If nPageNo = 1 Then
For Each oColumn As DataGridViewColumn In Grid1.Columns
nWidth = CType(Math.Floor(oColumn.Width / nTotalWidth * _
nTotalWidth * (e.MarginBounds.Width / nTotalWidth)), Int16)
nHeight = e.Graphics.MeasureString(oColumn.HeaderText, _
oColumn.InheritedStyle.Font, nWidth).Height + 11
oColumnLefts.Add(nLeft)
oColumnWidths.Add(nWidth)
oColumnTypes.Add(oColumn.GetType)
nLeft += nWidth
Next
End If
Do While nRowPos <= Grid1.Rows.Count - 1
Dim oRow As DataGridViewRow = Grid1.Rows(nRowPos)
If nTop + nHeight >= e.MarginBounds.Height + e.MarginBounds.Top Then
DrawFooter(e, nRowsPerPage)
NewPage = True
nPageNo += 1
e.HasMorePages = True
Exit Sub
Else
If NewPage Then
' Draw Header
e.Graphics.DrawString(Header, New Font(ÜSchriftart, _
FontStyle.Bold), Brushes.Black, e.MarginBounds.Left, _
e.MarginBounds.Top - e.Graphics.MeasureString(Header, New _
Font(ÜSchriftart, FontStyle.Bold), _
e.MarginBounds.Width).Height - 13)
' Draw Columns
nTop = e.MarginBounds.Top
i = 0
For Each oColumn As DataGridViewColumn In Grid1.Columns
e.Graphics.FillRectangle(New SolidBrush( _
Drawing.Color.LightGray), New Rectangle(oColumnLefts( _
i), nTop, oColumnWidths(i), nHeight))
e.Graphics.DrawRectangle(Pens.Black, New Rectangle( _
oColumnLefts(i), nTop, oColumnWidths(i), nHeight))
e.Graphics.DrawString(oColumn.HeaderText, _
oColumn.InheritedStyle.Font, New SolidBrush( _
oColumn.InheritedStyle.ForeColor), New RectangleF( _
oColumnLefts(i), nTop, oColumnWidths(i), nHeight), _
oStringFormat)
i += 1
Next
NewPage = False
End If
nTop += nHeight
i = 0
For Each oCell As DataGridViewCell In oRow.Cells
If oColumnTypes(i) Is GetType(DataGridViewTextBoxColumn) _
OrElse oColumnTypes(i) Is GetType(DataGridViewLinkColumn) _
Then
e.Graphics.DrawString(oCell.Value.ToString, _
oCell.InheritedStyle.Font, New SolidBrush( _
oCell.InheritedStyle.ForeColor), New RectangleF( _
oColumnLefts(i), nTop, oColumnWidths(i), nHeight), _
oStringFormat)
ElseIf oColumnTypes(i) Is GetType(DataGridViewButtonColumn) _
Then
oButton.Text = oCell.Value.ToString
oButton.Size = New Size(oColumnWidths(i), nHeight)
Dim oBitmap As New Bitmap(oButton.Width, oButton.Height)
oButton.DrawToBitmap(oBitmap, New Rectangle(0, 0, _
oBitmap.Width, oBitmap.Height))
e.Graphics.DrawImage(oBitmap, New Point(oColumnLefts( _
i), nTop)) |