|
-
Mar 20th, 2023, 12:39 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Adding and grouping datagrid view column header in VB.NET
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 :
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
And in the paint event :
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
However I also want to group columns 5,6 & 7 together and call that "Second Group".
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
-
Mar 21st, 2023, 06:20 AM
#2
Thread Starter
Addicted Member
Re: Adding and grouping datagrid view column header in VB.NET
For anyone interested - what I have done is:
Increase the width of the second group and draw it before the first group.
You may have to play with the heights and alignments a bit.
In the Paint event :
Code:
Dim width_secondgroup As Integer = w5 + w6 + w7
r5.Width = width_secondgroup - 1
r5.Height = r5.Height / 2 + 18
e.Graphics.FillRectangle(New SolidBrush(Me.ParsreelsDataGridView.ColumnHeadersDefaultCellStyle.BackColor), r5)
e.Graphics.DrawString("Penalisasie", Me.ParsreelsDataGridView.ColumnHeadersDefaultCellStyle.Font, New SolidBrush(Me.ParsreelsDataGridView.ColumnHeadersDefaultCellStyle.ForeColor), r5, format)
Dim width_firstgroup As Integer = w3 + w4 + w5 + w6 + w7
r3.Width = width_firstgroup - 1
r3.Height = r3.Height / 3 + 3
e.Graphics.FillRectangle(New SolidBrush(Me.ParsreelsDataGridView.ColumnHeadersDefaultCellStyle.BackColor), r3)
e.Graphics.DrawString("Oorspronklik", Me.ParsreelsDataGridView.ColumnHeadersDefaultCellStyle.Font, New SolidBrush(Me.ParsreelsDataGridView.ColumnHeadersDefaultCellStyle.ForeColor), r3, format)
The result is as in the image below :
Last edited by GideonE; Mar 21st, 2023 at 06:42 AM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|