PageRenderTime 24ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/Visual Studio 2008/VBWinFormDataGridView/MultipleLayeredColumnHeader/MainForm.vb

#
Visual Basic | 90 lines | 52 code | 16 blank | 22 comment | 0 complexity | 6a8a113633e92b0f4e2a7bd0f8cb0845 MD5 | raw file
  1. '************************************* Module Header **************************************\
  2. ' Module Name: MultipleLayeredColumnHeader
  3. ' Project: VBWinFormDataGridView
  4. ' Copyright (c) Microsoft Corporation.
  5. '
  6. ' This sample demonstrates how to display multiple layer column headers on the DataGridView.
  7. '
  8. ' This source is subject to the Microsoft Public License.
  9. ' See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
  10. ' All other rights reserved.
  11. '
  12. ' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  13. ' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
  14. ' WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  15. '**********************************************************************************/
  16. Namespace VBWinFormDataGridView.MultipleLayeredColumnHeader
  17. Public Class MainForm
  18. Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  19. Me.dataGridView1.Columns.Add("JanWin", "Win")
  20. Me.dataGridView1.Columns.Add("JanLoss", "Loss")
  21. Me.dataGridView1.Columns.Add("FebWin", "Win")
  22. Me.dataGridView1.Columns.Add("FebLoss", "Loss")
  23. Me.dataGridView1.Columns.Add("MarWin", "Win")
  24. Me.dataGridView1.Columns.Add("MarLoss", "Loss")
  25. For j As Integer = 0 To Me.dataGridView1.ColumnCount - 1
  26. Me.dataGridView1.Columns(j).Width = 45
  27. Next
  28. ' Enable resizing on the column headers
  29. Me.dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
  30. ' Adjust the height for the column headers
  31. Me.dataGridView1.ColumnHeadersHeight = Me.dataGridView1.ColumnHeadersHeight * 2
  32. ' Adjust the text alignment on the column headers to make the text display
  33. ' at the center of the bottom
  34. Me.dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter
  35. ' Handle the CellPainting event to draw text for each header cell
  36. End Sub
  37. Private Sub dataGridView1_CellPainting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles dataGridView1.CellPainting
  38. If e.RowIndex = -1 AndAlso e.ColumnIndex > -1 Then
  39. e.PaintBackground(e.CellBounds, False)
  40. Dim r2 As Rectangle = e.CellBounds
  41. r2.Y += e.CellBounds.Height / 2
  42. r2.Height = e.CellBounds.Height / 2
  43. e.PaintContent(r2)
  44. e.Handled = True
  45. End If
  46. End Sub
  47. Private Sub dataGridView1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles dataGridView1.Paint
  48. ' Data for the "merged" header cells
  49. Dim monthes As String() = {"January", "February", "March"}
  50. For j As Integer = 0 To Me.dataGridView1.ColumnCount - 1 Step 2
  51. ' Get the column header cell bounds
  52. Dim r1 As Rectangle = Me.dataGridView1.GetCellDisplayRectangle(j, -1, True)
  53. r1.X += 1
  54. r1.Y += 1
  55. r1.Width = r1.Width * 2 - 2
  56. r1.Height = r1.Height / 2 - 2
  57. Using br As SolidBrush = New SolidBrush(Me.dataGridView1.ColumnHeadersDefaultCellStyle.BackColor)
  58. e.Graphics.FillRectangle(br, r1)
  59. End Using
  60. Using p As Pen = New Pen(SystemColors.InactiveBorder)
  61. e.Graphics.DrawLine(p, r1.X, r1.Bottom, r1.Right, r1.Bottom)
  62. End Using
  63. Using format As StringFormat = New StringFormat()
  64. Using br As SolidBrush = New SolidBrush(Me.dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor)
  65. format.Alignment = StringAlignment.Center
  66. format.LineAlignment = StringAlignment.Center
  67. e.Graphics.DrawString(monthes(j / 2), Me.dataGridView1.ColumnHeadersDefaultCellStyle.Font, _
  68. br, r1, format)
  69. End Using
  70. End Using
  71. Next
  72. End Sub
  73. End Class
  74. End Namespace