I am trying to add grouped column headers to a bound datagrid view.
I want columns 3-7 grouped as "First Group". I have managed that with the following code :
In the cellpainting event :
And in the paint event :Code:Private Sub ParsreelsDataGridView_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles ParsreelsDataGridView.CellPainting If e.RowIndex = -1 AndAlso e.ColumnIndex > -1 Then Dim r2 As Rectangle = e.CellBounds r2.Y += e.CellBounds.Height / 3 r2.Height = e.CellBounds.Height / 3 e.PaintBackground(r2, True) e.PaintContent(r2) e.Handled = True End If End Sub
However I also want to group columns 5,6 & 7 together and call that "Second Group".Code:Private Sub ParsreelsDataGridView_Paint(sender As Object, e As PaintEventArgs) Handles ParsreelsDataGridView.Paint Dim j As Integer Dim format As New StringFormat() format.Alignment = StringAlignment.Center format.LineAlignment = StringAlignment.Center j = 3 Dim r3 As Rectangle = Me.ParsreelsDataGridView.GetCellDisplayRectangle(j, -1, True) Dim w3 As Integer = Me.ParsreelsDataGridView.GetCellDisplayRectangle(j, -1, True).Width j = 4 Dim r4 As Rectangle = Me.ParsreelsDataGridView.GetCellDisplayRectangle(j, -1, True) Dim w4 As Integer = Me.ParsreelsDataGridView.GetCellDisplayRectangle(j, -1, True).Width j = 5 Dim r5 As Rectangle = Me.ParsreelsDataGridView.GetCellDisplayRectangle(j, -1, True) Dim w5 As Integer = Me.ParsreelsDataGridView.GetCellDisplayRectangle(j, -1, True).Width j = 6 Dim r6 As Rectangle = Me.ParsreelsDataGridView.GetCellDisplayRectangle(j, -1, True) Dim w6 As Integer = Me.ParsreelsDataGridView.GetCellDisplayRectangle(j, -1, True).Width j = 7 Dim r7 As Rectangle = Me.ParsreelsDataGridView.GetCellDisplayRectangle(j, -1, True) Dim w7 As Integer = Me.ParsreelsDataGridView.GetCellDisplayRectangle(j, -1, True).Width Dim width_firstgroup As Integer = w3 + w4 + w5 + w6 + w7 r3.Width = width_firstgroup - 2 r3.Height = r3.Height / 3 - 2 e.Graphics.FillRectangle(New SolidBrush(Me.ParsreelsDataGridView.ColumnHeadersDefaultCellStyle.BackColor), r3) e.Graphics.DrawString("First Group", Me.ParsreelsDataGridView.ColumnHeadersDefaultCellStyle.Font, New SolidBrush(Me.ParsreelsDataGridView.ColumnHeadersDefaultCellStyle.ForeColor), r3, format) 'Dim width_secondgroup As Integer = w5 + w6 + w7 'r5.Width = width_secondgroup - 2 'r5.Height = r5.Height / 3 - 2 'e.Graphics.FillRectangle(New SolidBrush(Me.ParsreelsDataGridView.ColumnHeadersDefaultCellStyle.BackColor), r5) 'e.Graphics.DrawString("Second Group", Me.ParsreelsDataGridView.ColumnHeadersDefaultCellStyle.Font, New SolidBrush(Me.ParsreelsDataGridView.ColumnHeadersDefaultCellStyle.ForeColor), r5, format) End Sub
I cannot figure out how to let the second group display under the first group. In my code above (commented out lines),
I have managed to group the second group, but it displays over the first group.
Any help would be much appreciated.
Regards




Reply With Quote
