Public Partial Class DgvColumnHeaderMerge
Inherits Form
Public Sub New()
InitializeComponent()
End Sub
Private Sub DgvColumnHeaderMerge_Load(sender As Object, e As EventArgs)
Me.dataGridView1.Columns.Add("JanWin", "Win")
Me.dataGridView1.Columns.Add("JanLoss", "Loss")
Me.dataGridView1.Columns.Add("FebWin", "Win")
Me.dataGridView1.Columns.Add("FebLoss", "Loss")
Me.dataGridView1.Columns.Add("MarWin", "Win")
Me.dataGridView1.Columns.Add("MarLoss", "Loss")
Me.dataGridView1.Columns.Add("AprWin", "Win")
Me.dataGridView1.Columns.Add("AprLoss", "Loss")
Me.dataGridView1.Columns.Add("MayWin", "Win")
Me.dataGridView1.Columns.Add("MayLoss", "Loss")
For j As Integer = 0 To Me.dataGridView1.ColumnCount - 1
Me.dataGridView1.Columns(j).Width = 45
Next
Me.dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
Me.dataGridView1.ColumnHeadersHeight = Me.dataGridView1.ColumnHeadersHeight * 2
Me.dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter
Me.dataGridView1.CellPainting += New DataGridViewCellPaintingEventHandler(AddressOf dataGridView1_CellPainting)
Me.dataGridView1.Paint += New PaintEventHandler(AddressOf dataGridView1_Paint)
Me.dataGridView1.Scroll += New ScrollEventHandler(AddressOf dataGridView1_Scroll)
Me.dataGridView1.ColumnWidthChanged += New DataGridViewColumnEventHandler(AddressOf dataGridView1_ColumnWidthChanged)
End Sub
Private Sub dataGridView1_ColumnWidthChanged(sender As Object, e As DataGridViewColumnEventArgs)
Dim rtHeader As Rectangle = Me.dataGridView1.DisplayRectangle
rtHeader.Height = Me.dataGridView1.ColumnHeadersHeight / 2
Me.dataGridView1.Invalidate(rtHeader)
End Sub
Private Sub dataGridView1_Scroll(sender As Object, e As ScrollEventArgs)
Dim rtHeader As Rectangle = Me.dataGridView1.DisplayRectangle
rtHeader.Height = Me.dataGridView1.ColumnHeadersHeight / 2
Me.dataGridView1.Invalidate(rtHeader)
End Sub
Private Sub dataGridView1_Paint(sender As Object, e As PaintEventArgs)
Dim monthes As String() = {"January", "February", "March", "April", "May"}
Dim j As Integer = 0
While j < 10
Dim r1 As Rectangle = Me.dataGridView1.GetCellDisplayRectangle(j, -1, True)
Dim w2 As Integer = Me.dataGridView1.GetCellDisplayRectangle(j + 1, -1, True).Width
r1.X += 1
r1.Y += 1
r1.Width = r1.Width + w2 - 2
r1.Height = r1.Height / 2 - 2
e.Graphics.FillRectangle(New SolidBrush(Me.dataGridView1.ColumnHeadersDefaultCellStyle.BackColor), r1)
Dim format As New StringFormat()
format.Alignment = StringAlignment.Center
format.LineAlignment = StringAlignment.Center
e.Graphics.DrawString(monthes(j / 2), Me.dataGridView1.ColumnHeadersDefaultCellStyle.Font, New SolidBrush(Me.dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor), r1, format)
j += 2
End While
End Sub
Private Sub dataGridView1_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs)
If e.RowIndex = -1 AndAlso e.ColumnIndex > -1 Then
Dim r2 As Rectangle = e.CellBounds
r2.Y += e.CellBounds.Height / 2
r2.Height = e.CellBounds.Height / 2
e.PaintBackground(r2, True)
e.PaintContent(r2)
e.Handled = True
End If
End Sub
End Class
End Namespace