1. Imports System.Data.SqlClient
  2. Imports System.Collections.Generic
  3.  
  4. Public Class Form1
  5.  
  6. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  7.  
  8. Dim connectionString As String = "data source=SQLst; database=MVar2; User ID=Wep; Password=201ive; Integrated Security=false;"
  9.  
  10. Dim sql As String = "SELECT * FROM Priailer"
  11.  
  12. Dim connection As New SqlConnection(connectionString)
  13.  
  14. Dim dataadapter As New SqlDataAdapter(sql, connection)
  15.  
  16. Dim ds As New DataSet()
  17.  
  18. connection.Open()
  19.  
  20. dataadapter.Fill(ds, "Authors_table")
  21.  
  22. connection.Close()
  23.  
  24. DataGridView1.DataSource = ds
  25.  
  26. DataGridView1.DataMember = "Authors_table"
  27. DataGridView1.CurrentCell.Style.BackColor = Color.Red
  28.  
  29.  
  30. For m = 0 To 5
  31.  
  32.  
  33. For d = 0 To 3
  34. DataGridView1.Item(d, m).Style.BackColor = Color.Red
  35. Next d
  36.  
  37. Next m
  38.  
  39.  
  40.  
  41. End Sub
  42.  
  43.  
  44.  
  45. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  46.  
  47. Call PrintDGV.Print_DataGridView(DataGridView1)
  48.  
  49. End Sub
  50.  
  51.  
  52.  
  53.  
  54.  
  55. End Class
  56.  
  57.  
  58.  
  59.  
  60. ' Grid Object Printing - This routine was retrieved from an internet web-site (I don't remember which)
  61.  
  62. Public Class PrintDGV
  63.  
  64. Private Shared UseOldModel As Boolean = False
  65.  
  66. Private Shared StrFormat As StringFormat ' Holds content of a TextBox Cell to write by DrawString
  67. Private Shared StrFormatComboBox As StringFormat ' Holds content of a Boolean Cell to write by DrawImage
  68. Private Shared CellButton As Button ' Holds the Contents of Button Cell
  69. Private Shared CellCheckBox As CheckBox ' Holds the Contents of CheckBox Cell
  70. Private Shared CellComboBox As ComboBox ' Holds the Contents of ComboBox Cell
  71.  
  72. Private Shared TotalWidth As Int16 ' Summation of Columns widths
  73. Private Shared RowPos As Int16 ' Position of currently printing row
  74. Private Shared NewPage As Boolean ' Indicates if a new page reached
  75. Private Shared PageNo As Int16 ' Number of pages to print
  76. Private Shared ColumnLefts As New ArrayList ' Left Coordinate of Columns
  77. Private Shared ColumnWidths As New ArrayList ' Width of Columns
  78. Private Shared ColumnTypes As New ArrayList ' DataType of Columns
  79. Private Shared CellHeight As Int16 ' Height of DataGrid Cell
  80. Private Shared RowsPerPage As Int16 ' Number of Rows per Page
  81. Private Shared WithEvents PrintDoc As New System.Drawing.Printing.PrintDocument ' PrintDocumnet Object used for printing
  82.  
  83. Private Shared PrintTitle As String = "" ' Header of pages
  84. Private Shared dgv As DataGridView ' Holds DataGrid Object to print its contents
  85. Private Shared SelectedColumns As New List(Of String) ' The Columns Selected by user to print.
  86. Private Shared AvailableColumns As New List(Of String) ' All Columns avaiable in DataGrid
  87. Private Shared PrintAllRows As Boolean = True ' True = print all rows, False = print selected rows
  88. Private Shared FitToPageWidth As Boolean = True ' True = Fits selected columns to page width , False = Print columns as showed
  89. Private Shared HeaderHeight As Int16 = 0
  90.  
  91. 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
  92.  
  93.  
  94. Private Shared ColPage As Integer = 0
  95. Private Shared ColPageLimit As Integer = 100
  96.  
  97. Public Shared Sub Print_DataGridView(ByVal dgv1 As DataGridView)
  98. Dim ppvw As PrintPreviewDialog
  99. Try
  100. ' Getting DataGridView object to print
  101. dgv = dgv1
  102.  
  103. ColPageLimit = dgv1.ColumnCount
  104.  
  105. ' Getting all Coulmns Names in the DataGridView
  106. AvailableColumns.Clear()
  107. For Each c As DataGridViewColumn In dgv.Columns
  108. If Not c.Visible Then Continue For
  109. AvailableColumns.Add(c.HeaderText)
  110. Next
  111.  
  112. ' Showing the PrintOption Form
  113. Dim dlg As New PrintOptions(AvailableColumns)
  114. If dlg.ShowDialog() <> DialogResult.OK Then Exit Sub
  115.  
  116. ' Saving some printing attributes
  117. PrintTitle = dlg.PrintTitle
  118. PrintAllRows = dlg.PrintAllRows
  119. FitToPageWidth = dlg.FitToPageWidth
  120. SelectedColumns = dlg.GetSelectedColumns
  121.  
  122. RowsPerPage = 0
  123. PrintDoc.DefaultPageSettings.Landscape = True
  124. PrintDoc.DefaultPageSettings.Margins.Left = 10
  125. PrintDoc.DefaultPageSettings.Margins.Right = 10
  126.  
  127.  
  128. ppvw = New PrintPreviewDialog
  129. ppvw.Document = PrintDoc
  130.  
  131. ' Showing the Print Preview Page
  132. ppvw.Width = dgv1.Width
  133. ppvw.Height = dgv1.Height
  134.  
  135. If ppvw.Width > Screen.PrimaryScreen.WorkingArea.Width Then _
  136. ppvw.Width = Screen.PrimaryScreen.WorkingArea.Width
  137.  
  138. If ppvw.Height > Screen.PrimaryScreen.WorkingArea.Height Then _
  139. ppvw.Height = Screen.PrimaryScreen.WorkingArea.Height
  140.  
  141. If ppvw.ShowDialog() <> DialogResult.OK Then Exit Sub
  142. ' Printing the Documnet
  143. PrintDoc.Print()
  144. Catch ex As Exception
  145. MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  146. Finally
  147.  
  148. End Try
  149. End Sub
  150.  
  151. Private Shared Sub PrintDoc_BeginPrint(ByVal sender As Object, _
  152. ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDoc.BeginPrint
  153.  
  154. Try
  155. ' Formatting the Content of Text Cells to print
  156. StrFormat = New StringFormat
  157. StrFormat.Alignment = StringAlignment.Near
  158. StrFormat.LineAlignment = StringAlignment.Center
  159. StrFormat.Trimming = StringTrimming.EllipsisCharacter
  160.  
  161. ' Formatting the Content of Combo Cells to print
  162. StrFormatComboBox = New StringFormat
  163. StrFormatComboBox.LineAlignment = StringAlignment.Center
  164. StrFormatComboBox.FormatFlags = StringFormatFlags.NoWrap
  165. StrFormatComboBox.Trimming = StringTrimming.EllipsisCharacter
  166.  
  167. ColumnLefts.Clear()
  168. ColumnWidths.Clear()
  169. ColumnTypes.Clear()
  170. CellHeight = 0
  171. RowsPerPage = 0
  172.  
  173. ' For various column types
  174. CellButton = New Button
  175. CellCheckBox = New CheckBox
  176. CellComboBox = New ComboBox
  177.  
  178.  
  179. TotalWidth = 0
  180. For Each GridCol As DataGridViewColumn In dgv.Columns
  181. If Not GridCol.Visible Then Continue For
  182. If Not SelectedColumns.Contains(GridCol.HeaderText) Then Continue For
  183. TotalWidth += GridCol.Width
  184. Next
  185.  
  186. ColPages = 0
  187. Dim TheCols As Integer = 0
  188.  
  189. ReDim ColCount(ColPages)
  190. ColCount(ColPages) = 0
  191. ReDim ColPrint(ColPageLimit, TheCols), ColumnLeft(ColPageLimit, TheCols), ColumnType(ColPageLimit, TheCols), ColumnWidth(ColPageLimit, TheCols)
  192. HeaderCol = -1
  193.  
  194. Dim TmpWidth As Integer = 0, TmpLeft As Integer = 0
  195.  
  196. For Each GridCol As DataGridViewColumn In dgv.Columns
  197. If ColPages >= ColPageLimit Then
  198. Exit For
  199. End If
  200. If Not GridCol.Visible Then Continue For
  201. If Not SelectedColumns.Contains(GridCol.HeaderText) Then Continue For
  202.  
  203. If HeaderCol = -1 Then _
  204. HeaderCol = GridCol.Index
  205.  
  206. If FitToPageWidth Then
  207. TmpWidth = CType(Math.Floor(GridCol.Width / TotalWidth * _
  208. TotalWidth * (PrintDoc.DefaultPageSettings.PrintableArea.Height / TotalWidth)), Int16)
  209. TmpLeft += TmpWidth
  210. Else
  211. TmpWidth = GridCol.Width
  212. If TmpLeft + TmpWidth > PrintDoc.DefaultPageSettings.PrintableArea.Height Then
  213. ColPages += 1
  214. ReDim Preserve ColCount(ColPages)
  215. ColCount(ColPages) = 0
  216. ReDim Preserve ColPrint(ColPageLimit, TheCols), ColumnLeft(ColPageLimit, TheCols), ColumnType(ColPageLimit, TheCols), ColumnWidth(ColPageLimit, TheCols)
  217. If HeaderCol > -1 Then
  218. Dim GC As DataGridViewColumn = dgv.Columns(HeaderCol)
  219. ColumnLeft(ColPages, ColCount(ColPages)) = PrintDoc.DefaultPageSettings.Margins.Left
  220. ColumnWidth(ColPages, ColCount(ColPages)) = GC.Width
  221. ColPrint(ColPages, ColCount(ColPages)) = HeaderCol
  222. ColumnType(ColPages, ColCount(ColPages)) = GC.GetType
  223. ColCount(ColPages) += 1
  224. TmpLeft = GC.Width
  225. Else
  226. TmpLeft = 0
  227. End If
  228. End If
  229. TmpLeft += TmpWidth
  230. End If
  231.  
  232. If TheCols < ColCount(ColPages) Then
  233. TheCols = ColCount(ColPages)
  234. ReDim Preserve ColPrint(ColPageLimit, TheCols), ColumnLeft(ColPageLimit, TheCols), ColumnType(ColPageLimit, TheCols), ColumnWidth(ColPageLimit, TheCols)
  235. End If
  236.  
  237. ColumnWidth(ColPages, ColCount(ColPages)) = TmpWidth
  238. ColumnLeft(ColPages, ColCount(ColPages)) = TmpLeft - TmpWidth + PrintDoc.DefaultPageSettings.Margins.Left
  239. ColumnType(ColPages, ColCount(ColPages)) = GridCol.GetType
  240. ColPrint(ColPages, ColCount(ColPages)) = GridCol.Index
  241. ColCount(ColPages) += 1
  242. Next
  243.  
  244. ColPage = 0
  245. PageNo = 1
  246. NewPage = True
  247. RowPos = 0
  248. Catch ex As Exception
  249. MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  250. Finally
  251.  
  252. End Try
  253. End Sub
  254.  
  255.  
  256. Private Shared Sub PrintPage(ByVal e As System.Drawing.Printing.PrintPageEventArgs)
  257.  
  258. Dim tmpTop As Int16 = e.MarginBounds.Top
  259. Dim ColAt As Integer = 0, PageAt As Integer = 0
  260.  
  261. Try
  262.  
  263. If PageNo = 1 Then
  264. For PageAt = 0 To ColPages - 1
  265. For ColAt = 0 To ColCount(PageAt) - 1
  266. Dim GridCol As DataGridViewColumn = dgv.Columns(ColPrint(PageAt, ColAt))
  267. HeaderHeight = e.Graphics.MeasureString(GridCol.HeaderText, _
  268. GridCol.InheritedStyle.Font, ColumnWidth(PageAt, ColAt)).Height + 11
  269. Next
  270. Next
  271. End If
  272.  
  273. Do While RowPos <= dgv.Rows.Count - 1
  274. Dim GridRow As DataGridViewRow = dgv.Rows(RowPos)
  275. If GridRow.IsNewRow OrElse (Not PrintAllRows AndAlso Not GridRow.Selected) Then
  276. RowPos += 1 : Continue Do
  277. End If
  278.  
  279. CellHeight = GridRow.Height
  280.  
  281. If tmpTop + CellHeight >= e.MarginBounds.Height + e.MarginBounds.Top Then
  282. NewPage = True
  283. PageNo += 1
  284. e.HasMorePages = True
  285. Exit Sub
  286. Else
  287. If NewPage Then
  288. ' Draw Header
  289. e.Graphics.DrawString(PrintTitle, New Font(dgv.Font, FontStyle.Bold), _
  290. Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top - _
  291. e.Graphics.MeasureString(PrintTitle, New Font(dgv.Font, _
  292. FontStyle.Bold), e.MarginBounds.Width).Height - 13)
  293.  
  294. Dim s As String = Now.ToLongDateString + " " + Now.ToShortTimeString
  295.  
  296. e.Graphics.DrawString(s, New Font(dgv.Font, FontStyle.Bold), _
  297. Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - _
  298. e.Graphics.MeasureString(s, New Font(dgv.Font, FontStyle.Bold), _
  299. e.MarginBounds.Width).Width), e.MarginBounds.Top - _
  300. e.Graphics.MeasureString(PrintTitle, _
  301. New Font(New Font(dgv.Font, FontStyle.Bold), FontStyle.Bold), _
  302. e.MarginBounds.Width).Height - 13)
  303.  
  304. ' Draw Columns
  305. tmpTop = e.MarginBounds.Top
  306. For ColAt = 0 To ColCount(ColPage) - 1
  307. e.Graphics.FillRectangle(New SolidBrush(Drawing.Color.LightGray), _
  308. New Rectangle(ColumnLeft(ColPage, ColAt), tmpTop, ColumnWidth(ColPage, ColAt), HeaderHeight))
  309.  
  310. e.Graphics.DrawRectangle(Pens.Black, New Rectangle(ColumnLeft(ColPage, ColAt), _
  311. tmpTop, ColumnWidth(ColPage, ColAt), HeaderHeight))
  312.  
  313. Dim GridCol As DataGridViewColumn = dgv.Columns(ColPrint(ColPage, ColAt))
  314. e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font, _
  315. New SolidBrush(GridCol.InheritedStyle.ForeColor), _
  316. New RectangleF(ColumnLeft(ColPage, ColAt), tmpTop, ColumnWidth(ColPage, ColAt), _
  317. HeaderHeight), StrFormat)
  318.  
  319. Next
  320.  
  321. NewPage = False
  322.  
  323. tmpTop += HeaderHeight
  324. End If
  325.  
  326. For ColAt = 0 To ColCount(ColPage) - 1
  327. Dim Cel As DataGridViewCell = GridRow.Cells(ColPrint(ColPage, ColAt))
  328. If Cel.Value Is Nothing Then _
  329. Cel.Value = ""
  330.  
  331. If ColumnType(ColPage, ColAt) Is GetType(DataGridViewTextBoxColumn) OrElse ColumnType(ColPage, ColAt) Is GetType(DataGridViewLinkColumn) Then
  332. e.Graphics.DrawString(Cel.Value.ToString, Cel.InheritedStyle.Font, _
  333. New SolidBrush(Cel.InheritedStyle.ForeColor), _
  334. New RectangleF(ColumnLeft(ColPage, ColAt), tmpTop, ColumnWidth(ColPage, ColAt), _
  335. CellHeight), StrFormat)
  336.  
  337. ' For the Button Column
  338. ElseIf ColumnType(ColPage, ColAt) Is GetType(DataGridViewButtonColumn) Then
  339. CellButton.Text = Cel.Value.ToString
  340. CellButton.Size = New Size(ColumnWidth(ColPage, ColAt), CellHeight)
  341. Dim bmp As New Bitmap(CellButton.Width, CellButton.Height)
  342. CellButton.DrawToBitmap(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height))
  343. e.Graphics.DrawImage(bmp, New Point(ColumnLeft(ColPage, ColAt), tmpTop))
  344.  
  345. ' For the CheckBox Column
  346. ElseIf ColumnType(ColPage, ColAt) Is GetType(DataGridViewCheckBoxColumn) Then
  347.  
  348. CellCheckBox.Size = New Size(14, 14)
  349. CellCheckBox.Checked = CType(Cel.Value, Boolean)
  350. Dim bmp As New Bitmap(ColumnWidth(ColPage, ColAt), CellHeight)
  351. Dim tmpGraphics As Graphics = Graphics.FromImage(bmp)
  352. tmpGraphics.FillRectangle(Brushes.White, New Rectangle(0, 0, bmp.Width, bmp.Height))
  353. CellCheckBox.DrawToBitmap(bmp, New Rectangle(CType((bmp.Width - _
  354. CellCheckBox.Width) / 2, Int32), CType((bmp.Height - _
  355. CellCheckBox.Height) / 2, Int32), CellCheckBox.Width, _
  356. CellCheckBox.Height))
  357. e.Graphics.DrawImage(bmp, New Point(ColumnLeft(ColPage, ColAt), tmpTop))
  358.  
  359. ' For the ComboBox Column
  360. ElseIf ColumnType(ColPage, ColAt) Is GetType(DataGridViewComboBoxColumn) Then
  361.  
  362. CellComboBox.Size = New Size(ColumnWidth(ColPage, ColAt), CellHeight)
  363. Dim bmp As New Bitmap(CellComboBox.Width, CellComboBox.Height)
  364. CellComboBox.DrawToBitmap(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height))
  365. e.Graphics.DrawImage(bmp, New Point(ColumnLeft(ColPage, ColAt), tmpTop))
  366. e.Graphics.DrawString(Cel.Value.ToString, Cel.InheritedStyle.Font, _
  367. New SolidBrush(Cel.InheritedStyle.ForeColor), _
  368. New RectangleF(ColumnLeft(ColPage, ColAt) + 1, tmpTop, ColumnWidth(ColPage, ColAt) _
  369. - 16, CellHeight), StrFormatComboBox)
  370.  
  371. ' For the Image Column
  372. ElseIf ColumnType(ColPage, ColAt) Is GetType(DataGridViewImageColumn) Then
  373.  
  374. Dim CelSize As Rectangle = New Rectangle(ColumnLeft(ColPage, ColAt), tmpTop, ColumnWidth(ColPage, ColAt), CellHeight)
  375. Dim ImgSize As Size = CType(Cel.FormattedValue, Image).Size
  376. e.Graphics.DrawImage(Cel.FormattedValue, New Rectangle(ColumnLeft(ColPage, ColAt) _
  377. + CType(((CelSize.Width - ImgSize.Width) / 2), Int32), _
  378. tmpTop + CType(((CelSize.Height - ImgSize.Height) / 2), _
  379. Int32), CType(Cel.FormattedValue, Image).Width, CType(Cel.FormattedValue, _
  380. Image).Height))
  381.  
  382. End If
  383.  
  384. ' Drawing Cells Borders
  385. e.Graphics.DrawRectangle(Pens.Black, New Rectangle(ColumnLeft(ColPage, ColAt), tmpTop, ColumnWidth(ColPage, ColAt), CellHeight))
  386.  
  387. Next
  388. tmpTop += CellHeight
  389. End If
  390. RowPos += 1
  391. Loop
  392. ColPage += 1
  393. If ColPage <= ColPages Then
  394. e.HasMorePages = True
  395. NewPage = True
  396. RowPos = 0
  397. Else
  398. e.HasMorePages = False
  399. End If
  400.  
  401. Catch ex As Exception
  402.  
  403. MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  404. Finally
  405.  
  406. End Try
  407.  
  408. End Sub
  409.  
  410. Private Shared Sub PrintOldPage(ByVal e As System.Drawing.Printing.PrintPageEventArgs)
  411.  
  412. Dim tmpWidth As Int16, i As Int16
  413. Dim tmpTop As Int16 = e.MarginBounds.Top
  414. Dim tmpLeft As Int16 = e.MarginBounds.Left
  415.  
  416. Try
  417.  
  418. ' Before starting first page, it saves Width & Height of Headers and CoulmnType
  419. If PageNo = 1 Then
  420. For Each GridCol As DataGridViewColumn In dgv.Columns
  421. If Not GridCol.Visible Then Continue For
  422. If Not SelectedColumns.Contains(GridCol.HeaderText) Then Continue For
  423.  
  424. ' Detemining whether the columns are fitted to page or not.
  425. If FitToPageWidth Then
  426. tmpWidth = CType(Math.Floor(GridCol.Width / TotalWidth * _
  427. TotalWidth * (e.MarginBounds.Width / TotalWidth)), Int16)
  428. Else
  429. tmpWidth = GridCol.Width
  430. End If
  431. HeaderHeight = e.Graphics.MeasureString(GridCol.HeaderText, _
  432. GridCol.InheritedStyle.Font, tmpWidth).Height + 11
  433.  
  434. ColumnLefts.Add(tmpLeft)
  435. ColumnWidths.Add(tmpWidth)
  436. ColumnTypes.Add(GridCol.GetType)
  437. tmpLeft += tmpWidth
  438.  
  439. Next
  440. End If
  441.  
  442. ' Printing Current Page, Row by Row
  443. RowPos = 0
  444.  
  445. Do While RowPos <= dgv.Rows.Count - 1
  446. Dim GridRow As DataGridViewRow = dgv.Rows(RowPos)
  447. If GridRow.IsNewRow OrElse (Not PrintAllRows AndAlso Not GridRow.Selected) Then
  448. RowPos += 1 : Continue Do
  449. End If
  450.  
  451. CellHeight = GridRow.Height
  452.  
  453. If tmpTop + CellHeight >= e.MarginBounds.Height + e.MarginBounds.Top Then
  454. DrawFooter(e, RowsPerPage)
  455. NewPage = True
  456. PageNo += 1
  457. e.HasMorePages = True
  458. Exit Sub
  459. Else
  460. If NewPage Then
  461. ' Draw Header
  462. e.Graphics.DrawString(PrintTitle, New Font(dgv.Font, FontStyle.Bold), _
  463. Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top - _
  464. e.Graphics.MeasureString(PrintTitle, New Font(dgv.Font, _
  465. FontStyle.Bold), e.MarginBounds.Width).Height - 13)
  466.  
  467. Dim s As String = Now.ToLongDateString + " " + Now.ToShortTimeString
  468.  
  469. e.Graphics.DrawString(s, New Font(dgv.Font, FontStyle.Bold), _
  470. Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - _
  471. e.Graphics.MeasureString(s, New Font(dgv.Font, FontStyle.Bold), _
  472. e.MarginBounds.Width).Width), e.MarginBounds.Top - _
  473. e.Graphics.MeasureString(PrintTitle, _
  474. New Font(New Font(dgv.Font, FontStyle.Bold), FontStyle.Bold), _
  475. e.MarginBounds.Width).Height - 13)
  476.  
  477. ' Draw Columns
  478. tmpTop = e.MarginBounds.Top
  479. i = 0
  480.  
  481. For Each GridCol As DataGridViewColumn In dgv.Columns
  482. If Not GridCol.Visible Then Continue For
  483. If Not SelectedColumns.Contains(GridCol.HeaderText) Then Continue For
  484.  
  485. e.Graphics.FillRectangle(New SolidBrush(Drawing.Color.LightGray), _
  486. New Rectangle(ColumnLefts(i), tmpTop, ColumnWidths(i), HeaderHeight))
  487.  
  488. e.Graphics.DrawRectangle(Pens.Black, New Rectangle(ColumnLefts(i), _
  489. tmpTop, ColumnWidths(i), HeaderHeight))
  490.  
  491. e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font, _
  492. New SolidBrush(GridCol.InheritedStyle.ForeColor), _
  493. New RectangleF(ColumnLefts(i), tmpTop, ColumnWidths(i), _
  494. HeaderHeight), StrFormat)
  495. i += 1
  496. Next
  497. NewPage = False
  498.  
  499. tmpTop += HeaderHeight
  500. End If
  501.  
  502. i = 0
  503. For Each Cel As DataGridViewCell In GridRow.Cells
  504. If Not Cel.OwningColumn.Visible Then Continue For
  505. If Cel.Value Is Nothing Then
  506. Cel.Value = ""
  507. End If
  508.  
  509. If Not SelectedColumns.Contains(Cel.OwningColumn.HeaderText) Then
  510. Continue For
  511. End If
  512.  
  513. ' For the TextBox Column
  514. If ColumnTypes(i) Is GetType(DataGridViewTextBoxColumn) OrElse _
  515. ColumnTypes(i) Is GetType(DataGridViewLinkColumn) Then
  516.  
  517. e.Graphics.DrawString(Cel.Value.ToString, Cel.InheritedStyle.Font, _
  518. New SolidBrush(Cel.InheritedStyle.ForeColor), _
  519. New RectangleF(ColumnLefts(i), tmpTop, ColumnWidths(i), _
  520. CellHeight), StrFormat)
  521.  
  522. ' For the Button Column
  523. ElseIf ColumnTypes(i) Is GetType(DataGridViewButtonColumn) Then
  524.  
  525. CellButton.Text = Cel.Value.ToString
  526. CellButton.Size = New Size(ColumnWidths(i), CellHeight)
  527. Dim bmp As New Bitmap(CellButton.Width, CellButton.Height)
  528. CellButton.DrawToBitmap(bmp, New Rectangle(0, 0, _
  529. bmp.Width, bmp.Height))
  530. e.Graphics.DrawImage(bmp, New Point(ColumnLefts(i), tmpTop))
  531.  
  532. ' For the CheckBox Column
  533. ElseIf ColumnTypes(i) Is GetType(DataGridViewCheckBoxColumn) Then
  534.  
  535. CellCheckBox.Size = New Size(14, 14)
  536. CellCheckBox.Checked = CType(Cel.Value, Boolean)
  537. Dim bmp As New Bitmap(ColumnWidths(i), CellHeight)
  538. Dim tmpGraphics As Graphics = Graphics.FromImage(bmp)
  539. tmpGraphics.FillRectangle(Brushes.White, New Rectangle(0, 0, _
  540. bmp.Width, bmp.Height))
  541. CellCheckBox.DrawToBitmap(bmp, New Rectangle(CType((bmp.Width - _
  542. CellCheckBox.Width) / 2, Int32), CType((bmp.Height - _
  543. CellCheckBox.Height) / 2, Int32), CellCheckBox.Width, _
  544. CellCheckBox.Height))
  545. e.Graphics.DrawImage(bmp, New Point(ColumnLefts(i), tmpTop))
  546.  
  547. ' For the ComboBox Column
  548. ElseIf ColumnTypes(i) Is GetType(DataGridViewComboBoxColumn) Then
  549.  
  550. CellComboBox.Size = New Size(ColumnWidths(i), CellHeight)
  551. Dim bmp As New Bitmap(CellComboBox.Width, CellComboBox.Height)
  552. CellComboBox.DrawToBitmap(bmp, New Rectangle(0, 0, _
  553. bmp.Width, bmp.Height))
  554. e.Graphics.DrawImage(bmp, New Point(ColumnLefts(i), tmpTop))
  555. e.Graphics.DrawString(Cel.Value.ToString, Cel.InheritedStyle.Font, _
  556. New SolidBrush(Cel.InheritedStyle.ForeColor), _
  557. New RectangleF(ColumnLefts(i) + 1, tmpTop, ColumnWidths(i) _
  558. - 16, CellHeight), StrFormatComboBox)
  559.  
  560. ' For the Image Column
  561. ElseIf ColumnTypes(i) Is GetType(DataGridViewImageColumn) Then
  562.  
  563. Dim CelSize As Rectangle = New Rectangle(ColumnLefts(i), _
  564. tmpTop, ColumnWidths(i), CellHeight)
  565. Dim ImgSize As Size = CType(Cel.FormattedValue, Image).Size
  566. e.Graphics.DrawImage(Cel.FormattedValue, New Rectangle(ColumnLefts(i) _
  567. + CType(((CelSize.Width - ImgSize.Width) / 2), Int32), _
  568. tmpTop + CType(((CelSize.Height - ImgSize.Height) / 2), _
  569. Int32), CType(Cel.FormattedValue, Image).Width, CType(Cel.FormattedValue, _
  570. Image).Height))
  571.  
  572. End If
  573.  
  574. ' Drawing Cells Borders
  575. e.Graphics.DrawRectangle(Pens.Black, New Rectangle(ColumnLefts(i), _
  576. tmpTop, ColumnWidths(i), CellHeight))
  577.  
  578. i += 1
  579.  
  580. Next
  581. tmpTop += CellHeight
  582.  
  583. End If
  584.  
  585. RowPos += 1
  586. ' For the first page it calculates Rows per Page
  587. If PageNo = 1 Then
  588. RowsPerPage += 1
  589. End If
  590. Loop
  591.  
  592. If RowsPerPage = 0 Then Exit Sub
  593.  
  594. ' Write Footer (Page Number)
  595. DrawFooter(e, RowsPerPage)
  596.  
  597. e.HasMorePages = False
  598.  
  599.  
  600. Catch ex As Exception
  601. MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  602. Finally
  603.  
  604. End Try
  605.  
  606. End Sub
  607.  
  608. Private Shared Sub PrintDoc_PrintPage(ByVal sender As Object, _
  609. ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDoc.PrintPage
  610.  
  611. If UseOldModel Then
  612. PrintOldPage(e)
  613. Else
  614. PrintPage(e)
  615. End If
  616.  
  617. End Sub
  618.  
  619. Private Shared Sub DrawFooter(ByVal e As System.Drawing.Printing.PrintPageEventArgs, ByVal RowsPerPage As Int32)
  620. Dim cnt As Integer
  621.  
  622. ' Detemining rows number to print
  623. If PrintAllRows Then
  624. If dgv.Rows(dgv.Rows.Count - 1).IsNewRow Then
  625. ' When the DataGridView doesn't allow adding rows
  626. cnt = dgv.Rows.Count - 2
  627. Else
  628. ' When the DataGridView allows adding rows
  629. cnt = dgv.Rows.Count - 1
  630. End If
  631. Else
  632. cnt = dgv.SelectedRows.Count
  633. End If
  634.  
  635. ' Writing the Page Number on the Bottom of Page
  636. Dim PageNum As String = PageNo.ToString + " of " + _
  637. Math.Ceiling(cnt / RowsPerPage).ToString
  638. e.Graphics.DrawString(PageNum, dgv.Font, Brushes.Black, _
  639. e.MarginBounds.Left + (e.MarginBounds.Width - _
  640. e.Graphics.MeasureString(PageNum, dgv.Font, _
  641. e.MarginBounds.Width).Width) / 2, e.MarginBounds.Top + _
  642. e.MarginBounds.Height + 31)
  643.  
  644. End Sub
  645.  
  646. End Class