This following code demonstrates how to display multiple layer column headers on the DataGridView control:
'Enable resizing on the column headers
Me.DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
'Adjust the height for the column headers
Me.DataGridView1.ColumnHeadersHeight = Me.DataGridView1.ColumnHeadersHeight * 2
'Adjust the text alignment on the column headers to make the text display at the center of the bottom
Me.DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter
'Handle the DataGridView.CellPainting event to draw text for each header cell
Private Sub DataGridView1_CellPainting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
If e.RowIndex = -1 AndAlso e.ColumnIndex > -1 Then
e.PaintBackground(e.CellBounds, False)
Dim r2 As Rectangle = e.CellBounds
r2.Y += e.CellBounds.Height / 2
r2.Height = e.CellBounds.Height / 2
e.PaintContent(r2)
e.Handled = True
End If
End Sub
'Handle the DataGridView.Paint event to draw "merged" header cells
Private Sub DataGridView1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles DataGridView1.Paint
'Data for merged Header cells
Dim feeTitles As String() = {"Admission", "MonthlyFee", "Id Card"}
For j As Integer = 0 To Me.DataGridView1.ColumnCount - 1 Step 2
'Get the column header cell bounds
Dim r1 As Rectangle = Me.DataGridView1.GetCellDisplayRectangle(j, -1, True)
r1.X += 1
r1.Y += 1
r1.Width = r1.Width * 2 - 2
r1.Height = r1.Height / 2 - 2
Using br As SolidBrush = New SolidBrush(Me.DataGridView1.ColumnHeadersDefaultCellStyle.BackColor)
e.Graphics.FillRectangle(br, r1)
End Using
Using p As Pen = New Pen(SystemColors.InactiveBorder)
e.Graphics.DrawLine(p, r1.X, r1.Bottom, r1.Right, r1.Bottom)
End Using
Using format As StringFormat = New StringFormat()
Using br As SolidBrush = New SolidBrush(Me.DataGridView1.ColumnHeadersDefaultCellStyle.ForeColor)
format.Alignment = StringAlignment.Center
format.LineAlignment = StringAlignment.Center
e.Graphics.DrawString(feeTitles(j / 2), Me.DataGridView1.ColumnHeadersDefaultCellStyle.Font, Brushes.Brown, r1, format)
End Using
End Using
Next
End Sub
'Enable resizing on the column headers
Me.DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
'Adjust the height for the column headers
Me.DataGridView1.ColumnHeadersHeight = Me.DataGridView1.ColumnHeadersHeight * 2
'Adjust the text alignment on the column headers to make the text display at the center of the bottom
Me.DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter
'Handle the DataGridView.CellPainting event to draw text for each header cell
Private Sub DataGridView1_CellPainting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
If e.RowIndex = -1 AndAlso e.ColumnIndex > -1 Then
e.PaintBackground(e.CellBounds, False)
Dim r2 As Rectangle = e.CellBounds
r2.Y += e.CellBounds.Height / 2
r2.Height = e.CellBounds.Height / 2
e.PaintContent(r2)
e.Handled = True
End If
End Sub
'Handle the DataGridView.Paint event to draw "merged" header cells
Private Sub DataGridView1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles DataGridView1.Paint
'Data for merged Header cells
Dim feeTitles As String() = {"Admission", "MonthlyFee", "Id Card"}
For j As Integer = 0 To Me.DataGridView1.ColumnCount - 1 Step 2
'Get the column header cell bounds
Dim r1 As Rectangle = Me.DataGridView1.GetCellDisplayRectangle(j, -1, True)
r1.X += 1
r1.Y += 1
r1.Width = r1.Width * 2 - 2
r1.Height = r1.Height / 2 - 2
Using br As SolidBrush = New SolidBrush(Me.DataGridView1.ColumnHeadersDefaultCellStyle.BackColor)
e.Graphics.FillRectangle(br, r1)
End Using
Using p As Pen = New Pen(SystemColors.InactiveBorder)
e.Graphics.DrawLine(p, r1.X, r1.Bottom, r1.Right, r1.Bottom)
End Using
Using format As StringFormat = New StringFormat()
Using br As SolidBrush = New SolidBrush(Me.DataGridView1.ColumnHeadersDefaultCellStyle.ForeColor)
format.Alignment = StringAlignment.Center
format.LineAlignment = StringAlignment.Center
e.Graphics.DrawString(feeTitles(j / 2), Me.DataGridView1.ColumnHeadersDefaultCellStyle.Font, Brushes.Brown, r1, format)
End Using
End Using
Next
End Sub