Results 1 to 1 of 1

Thread: Problem with merged header cells in a datagridview

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2018
    Posts
    514

    Problem with merged header cells in a datagridview

    Hi
    After a long consideration I have chosen (stolen) a solution for this challenge of mine which I could more an less understand. However, I am unable to move the rectangle to the right position:

    Name:  2019-04-29_20-45-43.jpg
Views: 142
Size:  9.2 KB

    It should be placed here:

    Name:  2019-04-29_20-45-43_2.jpg
Views: 135
Size:  17.0 KB


    Here are my stolen codes which is based on the cell paint/painting. The trick is to increase the header cell height and place a rectangle over it.

    Code:
    Sub ApplyCount(ThisTeamOrGroup As String)
    
                    Dim FullTable As DataTable = GetMyDataTable_SP(ThisTeamOrGroup, TeamOrGroup, "MySP_Get_LOB_QA_Counts")
    
            DataGridView3.DataSource = FullTable
    
            If FullTable.Rows.Count > 0 Then
    
                MergedStuff = Nothing ' Public string() variable
    
                ReDim MergedStuff(FullTable.Rows.Count - 1)
    
                'getting the department names
                Dim AC As Integer = 0
                For Each Drow As DataRow In FullTable.Rows
                    MergedStuff(AC) = Drow.Item("Department")
                    AC = AC + 1
                Next Drow
    
                Dim TempTable As New DataTable
                TempTable = New DataTable("TableName")
    
                'add one column for phases
                TempTable.Columns.Add("Phase", GetType(String))
                ' add two columns per row (per team)
                For i As Integer = 0 To FullTable.Rows.Count - 1
                    TempTable.Columns.Add("LOB" & i, GetType(String))
                    TempTable.Columns.Add("QA" & i, GetType(String))
                    TempTable.Columns("LOB" & i).Caption = "At_LOB"
                    TempTable.Columns("QA" & i).Caption = "At_QA"
    
                Next i
    
                Dim Phases As String() = {"Past due", "Past owner due", "Due within 7 days", "Due within 14 days"}
                'insert values for each row
                For m As Integer = 1 To (FullTable.Columns.Count - 3) / 2
                    Dim NewRow As DataRow = TempTable.NewRow
                    NewRow("Phase") = Phases(m - 1)
                    Dim Counter As Integer = 0
                    For Each Drow As DataRow In FullTable.Rows
                        NewRow("LOB" & Counter) = Drow.Item(m * 2 - 1).ToString
                        NewRow("QA" & Counter) = Drow.Item(m * 2).ToString
                        Counter = Counter + 1
                    Next Drow
                    TempTable.Rows.Add(NewRow)
                Next m
    
                'DataGridView1.Columns(1).Width = 45
                For i As Integer = 1 To DataGridView1.Columns.Count - 1
                    DataGridView1.Columns(i).Width = 30
                Next i
    
                With DataGridView1
                    .DataSource = Nothing
                    .ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
                    .ColumnHeadersHeight = DataGridView1.ColumnHeadersHeight * 2
                    .ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter
                    .RowHeadersVisible = False
                    .AllowUserToAddRows = False
                    .AllowUserToResizeColumns = False
                    .AllowUserToResizeRows = False
                    .DataSource = TempTable
                End With
    
                For k As Integer = 0 To DataGridView1.Columns.Count - 1
                    DataGridView1.Columns(k).HeaderText = TempTable.Columns(k).Caption
                Next
                EnableDoubleBuffered(DataGridView1)
    
            End If
    
        End Sub
    
    Private Sub DataGridView1_Paint(sender As Object, e As PaintEventArgs) Handles DataGridView1.Paint
            For i As Integer = 0 To DataGridView1.Columns.Count - 1
                Dim RAngle As Rectangle = DataGridView1.GetCellDisplayRectangle(i, -1, True)
                Dim w2 As Integer = DataGridView1.GetCellDisplayRectangle(i, -1, True).Width
                RAngle.X += 1
                RAngle.Y += 1
                RAngle.Width = RAngle.Width + w2 - 2
                RAngle.Height = RAngle.Height / 2 - 2
    
                e.Graphics.FillRectangle(New SolidBrush(DataGridView1.ColumnHeadersDefaultCellStyle.BackColor), RAngle)
    
                Dim Format As StringFormat = New StringFormat()
                Format.Alignment = StringAlignment.Center
                Format.LineAlignment = StringAlignment.Center
                e.Graphics.DrawString(MergedStuff(i / 2), DataGridView1.ColumnHeadersDefaultCellStyle.Font,
                                      New SolidBrush(DataGridView1.ColumnHeadersDefaultCellStyle.ForeColor),
                                      RAngle,
                                      Format)
                i += 2
            Next
    
        End Sub
    
        Private Sub DataGridView1_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
    
            If (e.RowIndex - 1 And 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
    
        Private Sub DataGridView1_ColumnWidthChanged(sender As Object, e As DataGridViewColumnEventArgs) Handles DataGridView1.ColumnWidthChanged
            Dim rtHeader As Rectangle = DataGridView1.DisplayRectangle
    
            rtHeader.Height = DataGridView1.ColumnHeadersHeight / 2
            DataGridView1.Invalidate(rtHeader)
        End Sub
    
        Private Sub DataGridView1_Scroll(sender As Object, e As ScrollEventArgs) Handles DataGridView1.Scroll
            Dim rtHeader As Rectangle = DataGridView1.DisplayRectangle
    
            rtHeader.Height = DataGridView1.ColumnHeadersHeight / 2
            DataGridView1.Invalidate(rtHeader)
    
        End Sub
    The first part is to get the result from SQL table, manipulate it to fit into a new table and set it as the datagridview source. The funny stuff starts just after that with paint thing.


    sample table:

    Name:  2019-04-29_20-15-21.jpg
Views: 142
Size:  22.5 KB

    My problem is specifically here:

    Code:
        Private Sub DataGridView1_Paint(sender As Object, e As PaintEventArgs) Handles DataGridView1.Paint
           
            For i As Integer = 0 To DataGridView1.Columns.Count - 1
                Dim RAngle As Rectangle = DataGridView1.GetCellDisplayRectangle(i, -1, True)
                Dim w2 As Integer = DataGridView1.GetCellDisplayRectangle(i, -1, True).Width
                RAngle.X += 1
                RAngle.Y += 1
                RAngle.Width = RAngle.Width + w2 - 2
                RAngle.Height = RAngle.Height / 2 - 2
    
                e.Graphics.FillRectangle(New SolidBrush(DataGridView1.ColumnHeadersDefaultCellStyle.BackColor), RAngle)
    
                Dim Format As StringFormat = New StringFormat()
                Format.Alignment = StringAlignment.Center
                Format.LineAlignment = StringAlignment.Center
                e.Graphics.DrawString(MergedStuff(i / 2), DataGridView1.ColumnHeadersDefaultCellStyle.Font,
                                      New SolidBrush(DataGridView1.ColumnHeadersDefaultCellStyle.ForeColor),
                                      RAngle,
                                      Format)
                i += 2
            Next
    
        End Sub
    Thanks in advance and sorry for the long post.
    Last edited by Grand; Apr 29th, 2019 at 01:50 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width