I made some changes that fit to my requirements.
Sample Output
Codes:
Private Sub AddGridViews()
Dim subColumnHeader As String() = {"Fabric", "XS", "S", "M", "L", "XL"}
Dim dgView As DataGridView
FlowLayoutPanel1.SuspendLayout()
For i As Integer = 1 To 5
dgView = New DataGridView
With dgView
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter
.Size = New Size(307, 170)
.ColumnHeadersHeight = 40
For Each col As String In subColumnHeader
.Columns.Add("col" & col, col)
Next
.Rows.Add(3)
.Tag = "Fabric " & i ' holds the header of the grid
End With
AddHandler dgView.CellPainting, AddressOf dataGridView1_CellPainting
AddHandler dgView.Paint, AddressOf dataGridView1_Paint
FlowLayoutPanel1.Controls.Add(dgView)
Next
FlowLayoutPanel1.ResumeLayout()
End Sub
Private Sub dataGridView1_CellPainting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs)
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
Private Sub dataGridView1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs)
' Data for the "merged" header cells
Dim obj = TryCast(sender, DataGridView)
' Get the column header cell bounds
Dim r1 As Rectangle = obj.GetCellDisplayRectangle(0, -1, True)
r1.X += 1
r1.Y += 1
r1.Width = r1.Width * 6 - 2
r1.Height = r1.Height / 2 - 2
e.Graphics.FillRectangle(Brushes.Beige, r1)
e.Graphics.DrawLine(Pens.BurlyWood, r1.X, r1.Bottom, r1.Right, r1.Bottom)
Using format As StringFormat = New StringFormat()
Using br As SolidBrush = New SolidBrush(obj.ColumnHeadersDefaultCellStyle.ForeColor)
format.Alignment = StringAlignment.Center
format.LineAlignment = StringAlignment.Center
e.Graphics.DrawString(obj.Tag.ToString, obj.ColumnHeadersDefaultCellStyle.Font, _
br, r1, format)
End Using
End Using
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
AddGridViews()
End Sub
Hope this will help someone out there.
Thanks to Gaurav Khanna.
No comments:
Post a Comment