본문 바로가기
카테고리 없음

[vb.net] DataGridView 헤더 병합

by IT HUB 2021. 5. 2.
728x90
반응형

        dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing

        dgv.ColumnHeadersHeight *= 2

        AddHandler dgv.Paint, Sub(ByVal sd As ObjectByVal ee As PaintEventArgs)

                                  Dim monthes As String() = {"Total""Correct""Wrong""Score"}

                                  Dim j As Integer = 3

                                  Dim pDGV As DataGridView = CType(sd, DataGridView)

 

                                  Try

                                      While j < 18

                                          Dim r1 As Rectangle = pDGV.GetCellDisplayRectangle(j, -1True)

                                          Dim w2 As Integer = pDGV.GetCellDisplayRectangle(j + 1-1True).Width + pDGV.GetCellDisplayRectangle(j + 2-1True).Width + pDGV.GetCellDisplayRectangle(j + 3-1True).Width

                                          r1.X += 1

                                          r1.Y += 1

                                          r1.Width = r1.Width + w2 - 2

                                          r1.Height = r1.Height / 2 - 2

                                          '// e.Graphics.FillRectangle(New SolidBrush(DGV.ColumnHeadersDefaultCellStyle.BackColor), r1)

                                          If monthes((j - 3/ 4= "Score" Then

                                              ee.Graphics.FillRectangle(New SolidBrush(Color.Black), r1)

                                          Else

                                              ee.Graphics.FillRectangle(New SolidBrush(Color.DarkGray), r1)

                                          End If

                                          ee.Graphics.DrawLine(New Pen(New SolidBrush(Color.LightCyan)), r1.Left, r1.Bottom - 1, r1.Right, r1.Bottom - 1)

                                          Dim format As New StringFormat()

                                          format.Alignment = StringAlignment.Center

                                          format.LineAlignment = StringAlignment.Center

                                          ee.Graphics.DrawString(monthes((j - 3/ 4), pDGV.ColumnHeadersDefaultCellStyle.Font, New SolidBrush(Color.White), r1, format)

                                          j += 4

                                      End While

                                  Catch ex As Exception

                                  End Try

                              End Sub

반응형

댓글