0 Replies - 323 Views - Last Post: 03 May 2019 - 06:23 AM Rate Topic: -----

#1 Grander   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 15-November 18

Issue with "merged" headercell of a datagridview

Posted 03 May 2019 - 06:23 AM

Hi
I am trying sort of "merge" header cells of a datagridviw but I have a little “moving” issue with rectangles (box) that is drawn on the header cells. The boxes are drawn “OK” (except for the first one) but when users horizontally scroll the datagridview e.g. to right, all boxes move to the left (correct way) the first box (from left, on column 2 and 3) hold position and moves to the right instead. This applys also for the otehr boxes as soon as they reach the first two cloumns when scrolling.


Here is the code I got:


    Private Sub DataGridView1_Paint(sender As Object, e As PaintEventArgs) Handles DataGridView1.Paint
        Dim Counter As Integer = 0
        For i As Integer = 0 To DataGridView1.Columns.Count - 1 Step 2
            Dim RAngle As Rectangle = DataGridView1.GetCellDisplayRectangle(i, -1, True)
            Dim RWidth As Integer = DataGridView1.GetCellDisplayRectangle(i, -1, True).Width
            RAngle.X += 1 + RWidth
            RAngle.Y += 1
            RAngle.Width = RAngle.Width + RWidth - 2
            RAngle.Height = RAngle.Height / 2 - 2

            e.Graphics.FillRectangle(New SolidBrush(Color.LightGray), RAngle)

            Dim Format As StringFormat = New StringFormat()
            Format.Alignment = StringAlignment.Center
            Format.LineAlignment = StringAlignment.Center

‘ MergedStuff is a public array containing some names
            Dim thisLen As Integer = MergedStuff.Length
            e.Graphics.DrawString(MergedStuff(Counter),
            DataGridView1.ColumnHeadersDefaultCellStyle.Font,
            New SolidBrush(Color.Black),
            RAngle,
            Format)

            If i > 0 Then
                Counter = Counter + 1
            End If

        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 RAngle As Rectangle = e.CellBounds
            RAngle.Y += e.CellBounds.Height / 2
            RAngle.Height = e.CellBounds.Height / 2
            e.PaintBackground(RAngle, True)
            e.PaintContent(RAngle)
            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



Thanks in advance for any help.

This post has been edited by Grander: 03 May 2019 - 06:24 AM


Is This A Good Question/Topic? 0
  • +

Page 1 of 1