Advertisement
Guest User

GridView

a guest
Aug 20th, 2012
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.09 KB | None | 0 0
  1. Private Shared Sub PrintPage(ByVal e As System.Drawing.Printing.PrintPageEventArgs)
  2.  
  3. Dim tmpTop As Int16 = e.MarginBounds.Top
  4. Dim ColAt As Integer = 0, PageAt As Integer = 0
  5.  
  6. Try
  7.  
  8. If PageNo = 1 Then
  9. For PageAt = 0 To ColPages - 1
  10. For ColAt = 0 To ColCount(PageAt) - 1
  11. Dim GridCol As DataGridViewColumn = dgv.Columns(ColPrint(PageAt, ColAt))
  12. HeaderHeight = e.Graphics.MeasureString(GridCol.HeaderText, _
  13. GridCol.InheritedStyle.Font, ColumnWidth(PageAt, ColAt)).Height + 11
  14. Next
  15. Next
  16. End If
  17.  
  18. Do While RowPos <= dgv.Rows.Count - 1
  19. Dim GridRow As DataGridViewRow = dgv.Rows(RowPos)
  20. If GridRow.IsNewRow OrElse (Not PrintAllRows AndAlso Not GridRow.Selected) Then
  21. RowPos += 1 : Continue Do
  22. End If
  23.  
  24. CellHeight = GridRow.Height
  25.  
  26. If tmpTop + CellHeight >= e.MarginBounds.Height + e.MarginBounds.Top Then
  27. NewPage = True
  28. PageNo += 1
  29. e.HasMorePages = True
  30. Exit Sub
  31. Else
  32. If NewPage Then
  33. ' Draw Header
  34. e.Graphics.DrawString(PrintTitle, New Font(dgv.Font, FontStyle.Bold), _
  35. Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top - _
  36. e.Graphics.MeasureString(PrintTitle, New Font(dgv.Font, _
  37. FontStyle.Bold), e.MarginBounds.Width).Height - 13)
  38.  
  39. Dim s As String = Now.ToLongDateString + " " + Now.ToShortTimeString
  40.  
  41. e.Graphics.DrawString(s, New Font(dgv.Font, FontStyle.Bold), _
  42. Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - _
  43. e.Graphics.MeasureString(s, New Font(dgv.Font, FontStyle.Bold), _
  44. e.MarginBounds.Width).Width), e.MarginBounds.Top - _
  45. e.Graphics.MeasureString(PrintTitle, _
  46. New Font(New Font(dgv.Font, FontStyle.Bold), FontStyle.Bold), _
  47. e.MarginBounds.Width).Height - 13)
  48.  
  49. ' Draw Columns
  50. tmpTop = e.MarginBounds.Top
  51. For ColAt = 0 To ColCount(ColPage) - 1
  52. e.Graphics.FillRectangle(New SolidBrush(Drawing.Color.LightGray), _
  53. New Rectangle(ColumnLeft(ColPage, ColAt), tmpTop, ColumnWidth(ColPage, ColAt), HeaderHeight))
  54.  
  55. e.Graphics.DrawRectangle(Pens.Black, New Rectangle(ColumnLeft(ColPage, ColAt), _
  56. tmpTop, ColumnWidth(ColPage, ColAt), HeaderHeight))
  57.  
  58. Dim GridCol As DataGridViewColumn = dgv.Columns(ColPrint(ColPage, ColAt))
  59. e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font, _
  60. New SolidBrush(GridCol.InheritedStyle.ForeColor), _
  61. New RectangleF(ColumnLeft(ColPage, ColAt), tmpTop, ColumnWidth(ColPage, ColAt), _
  62. HeaderHeight), StrFormat)
  63.  
  64. Next
  65.  
  66. NewPage = False
  67.  
  68. tmpTop += HeaderHeight
  69. End If
  70. Dim MyAlpha As Integer
  71. Dim MyRed As Integer
  72. Dim MyGreen As Integer
  73. Dim MyBlue As Integer
  74.  
  75. For ColAt = 0 To ColCount(ColPage) - 1
  76. Dim Cel As DataGridViewCell = GridRow.Cells(ColPrint(ColPage, ColAt))
  77. If Cel.Value Is Nothing Then _
  78. Cel.Value = ""
  79.  
  80. If ColumnType(ColPage, ColAt) Is GetType(DataGridViewTextBoxColumn) OrElse ColumnType(ColPage, ColAt) Is GetType(DataGridViewLinkColumn) Then
  81. e.Graphics.DrawString(Cel.Value.ToString, Cel.InheritedStyle.Font, _
  82. New SolidBrush(Cel.InheritedStyle.ForeColor), _
  83. New RectangleF(ColumnLeft(ColPage, ColAt), tmpTop, ColumnWidth(ColPage, ColAt), _
  84. CellHeight), StrFormat)
  85.  
  86. ' For the Button Column
  87. ElseIf ColumnType(ColPage, ColAt) Is GetType(DataGridViewButtonColumn) Then
  88. CellButton.Text = Cel.Value.ToString
  89. CellButton.Size = New Size(ColumnWidth(ColPage, ColAt), CellHeight)
  90. Dim bmp As New Bitmap(CellButton.Width, CellButton.Height)
  91. CellButton.DrawToBitmap(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height))
  92. e.Graphics.DrawImage(bmp, New Point(ColumnLeft(ColPage, ColAt), tmpTop))
  93.  
  94. ' For the CheckBox Column
  95. ElseIf ColumnType(ColPage, ColAt) Is GetType(DataGridViewCheckBoxColumn) Then
  96.  
  97. CellCheckBox.Size = New Size(14, 14)
  98. CellCheckBox.Checked = CType(Cel.Value, Boolean)
  99. Dim bmp As New Bitmap(ColumnWidth(ColPage, ColAt), CellHeight)
  100. Dim tmpGraphics As Graphics = Graphics.FromImage(bmp)
  101. tmpGraphics.FillRectangle(Brushes.White, New Rectangle(0, 0, bmp.Width, bmp.Height))
  102. CellCheckBox.DrawToBitmap(bmp, New Rectangle(CType((bmp.Width - _
  103. CellCheckBox.Width) / 2, Int32), CType((bmp.Height - _
  104. CellCheckBox.Height) / 2, Int32), CellCheckBox.Width, _
  105. CellCheckBox.Height))
  106. e.Graphics.DrawImage(bmp, New Point(ColumnLeft(ColPage, ColAt), tmpTop))
  107.  
  108. ' For the ComboBox Column
  109. ElseIf ColumnType(ColPage, ColAt) Is GetType(DataGridViewComboBoxColumn) Then
  110.  
  111. CellComboBox.Size = New Size(ColumnWidth(ColPage, ColAt), CellHeight)
  112. Dim bmp As New Bitmap(CellComboBox.Width, CellComboBox.Height)
  113. CellComboBox.DrawToBitmap(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height))
  114. e.Graphics.DrawImage(bmp, New Point(ColumnLeft(ColPage, ColAt), tmpTop))
  115. e.Graphics.DrawString(Cel.Value.ToString, Cel.InheritedStyle.Font, _
  116. New SolidBrush(Cel.InheritedStyle.ForeColor), _
  117. New RectangleF(ColumnLeft(ColPage, ColAt) + 1, tmpTop, ColumnWidth(ColPage, ColAt) _
  118. - 16, CellHeight), StrFormatComboBox)
  119.  
  120. ' For the Image Column
  121. ElseIf ColumnType(ColPage, ColAt) Is GetType(DataGridViewImageColumn) Then
  122.  
  123. Dim CelSize As Rectangle = New Rectangle(ColumnLeft(ColPage, ColAt), tmpTop, ColumnWidth(ColPage, ColAt), CellHeight)
  124. Dim ImgSize As Size = CType(Cel.FormattedValue, Image).Size
  125. e.Graphics.DrawImage(Cel.FormattedValue, New Rectangle(ColumnLeft(ColPage, ColAt) _
  126. + CType(((CelSize.Width - ImgSize.Width) / 2), Int32), _
  127. tmpTop + CType(((CelSize.Height - ImgSize.Height) / 2), _
  128. Int32), CType(Cel.FormattedValue, Image).Width, CType(Cel.FormattedValue, _
  129. Image).Height))
  130.  
  131. End If
  132.  
  133. ' Initialize the random-number generator.
  134. Randomize()
  135. ' Generate random value between 1 and 6.
  136. MyAlpha = CInt(Int((254 * Rnd()) + 0))
  137. ' Initialize the random-number generator.
  138. Randomize()
  139. ' Generate random value between 1 and 6.
  140. MyRed = CInt(Int((254 * Rnd()) + 0))
  141. ' Initialize the random-number generator.
  142. Randomize()
  143. ' Generate random value between 1 and 6.
  144. MyGreen = CInt(Int((254 * Rnd()) + 0))
  145. ' Initialize the random-number generator.
  146. Randomize()
  147. ' Generate random value between 1 and 6.
  148. MyBlue = CInt(Int((254 * Rnd()) + 0))
  149.  
  150.  
  151. Dim penColor As New Pen(Color.FromArgb(MyAlpha, MyRed, MyGreen, MyBlue))
  152. ' Drawing Cells Borders
  153. e.Graphics.DrawRectangle(penColor, New Rectangle(ColumnLeft(ColPage, ColAt), tmpTop, ColumnWidth(ColPage, ColAt), CellHeight))
  154.  
  155. Next
  156. tmpTop += CellHeight
  157. End If
  158. RowPos += 1
  159. Loop
  160. ColPage += 1
  161. If ColPage <= ColPages Then
  162. e.HasMorePages = True
  163. NewPage = True
  164. RowPos = 0
  165. Else
  166. e.HasMorePages = False
  167. End If
  168.  
  169. Catch ex As Exception
  170.  
  171. MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  172. Finally
  173.  
  174. End Try
  175.  
  176. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement