PageRenderTime 12ms CodeModel.GetById 5ms app.highlight 5ms RepoModel.GetById 1ms 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
17Namespace VBWinFormDataGridView.MultipleLayeredColumnHeader
18
19    Public Class MainForm
20
21        Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
22            Me.dataGridView1.Columns.Add("JanWin", "Win")
23            Me.dataGridView1.Columns.Add("JanLoss", "Loss")
24            Me.dataGridView1.Columns.Add("FebWin", "Win")
25            Me.dataGridView1.Columns.Add("FebLoss", "Loss")
26            Me.dataGridView1.Columns.Add("MarWin", "Win")
27            Me.dataGridView1.Columns.Add("MarLoss", "Loss")
28
29            For j As Integer = 0 To Me.dataGridView1.ColumnCount - 1
30                Me.dataGridView1.Columns(j).Width = 45
31            Next
32
33            ' Enable resizing on the column headers
34            Me.dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
35
36            ' Adjust the height for the column headers
37            Me.dataGridView1.ColumnHeadersHeight = Me.dataGridView1.ColumnHeadersHeight * 2
38
39            ' Adjust the text alignment on the column headers to make the text display
40            ' at the center of the bottom
41            Me.dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter
42
43            ' Handle the CellPainting event to draw text for each header cell
44
45        End Sub
46
47        Private Sub dataGridView1_CellPainting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles dataGridView1.CellPainting
48            If e.RowIndex = -1 AndAlso e.ColumnIndex > -1 Then
49                e.PaintBackground(e.CellBounds, False)
50                Dim r2 As Rectangle = e.CellBounds
51                r2.Y += e.CellBounds.Height / 2
52                r2.Height = e.CellBounds.Height / 2
53                e.PaintContent(r2)
54                e.Handled = True
55            End If
56        End Sub
57
58        Private Sub dataGridView1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles dataGridView1.Paint
59            ' Data for the "merged" header cells
60            Dim monthes As String() = {"January", "February", "March"}
61            For j As Integer = 0 To Me.dataGridView1.ColumnCount - 1 Step 2
62                ' Get the column header cell bounds
63                Dim r1 As Rectangle = Me.dataGridView1.GetCellDisplayRectangle(j, -1, True)
64
65                r1.X += 1
66                r1.Y += 1
67                r1.Width = r1.Width * 2 - 2
68                r1.Height = r1.Height / 2 - 2
69
70                Using br As SolidBrush = New SolidBrush(Me.dataGridView1.ColumnHeadersDefaultCellStyle.BackColor)
71                    e.Graphics.FillRectangle(br, r1)
72                End Using
73
74                Using p As Pen = New Pen(SystemColors.InactiveBorder)
75                    e.Graphics.DrawLine(p, r1.X, r1.Bottom, r1.Right, r1.Bottom)
76                End Using
77
78                Using format As StringFormat = New StringFormat()
79                    Using br As SolidBrush = New SolidBrush(Me.dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor)
80                        format.Alignment = StringAlignment.Center
81                        format.LineAlignment = StringAlignment.Center
82                        e.Graphics.DrawString(monthes(j / 2), Me.dataGridView1.ColumnHeadersDefaultCellStyle.Font, _
83                                              br, r1, format)
84                    End Using
85                End Using
86            Next
87        End Sub
88    End Class
89
90End Namespace