DataGridViewの列ヘッダーの結合(しているように見せる) - VB.NET
※注意 この方法はやめた方がいい。横にスクロールすると表示がおかしくなるし、行追加するたびに表示がおかしくなる。
' DataGridViewの列の結合の配列 Private _mergeColumns(,) As Integer = New Integer(,) {{1, 2}, {4, 5}} ''' <summary> ''' 列を結合しているように見せる ''' </summary> ''' <param name="sender"></param> ''' <param name="e"></param> Private Sub DataGridView1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles DataGridView1.Paint Try For i As Integer = 0 To _mergeColumns.Length - 1 Dim leftIndex As Integer = _mergeColumns(i, 0) Dim rightIndex As Integer = _mergeColumns(i, 1) ' rowIndex が -1 は列ヘッダーセルっていう意味 Dim r1 As Rectangle = Me.DataGridView1.GetCellDisplayRectangle(leftIndex, -1, True) ' 次の列ヘッダーセルの横幅を取得する Dim w2 As Integer = Me.DataGridView1.GetCellDisplayRectangle(rightIndex, -1, True).Width r1.Width += w2 - 2 ' 指定の色で塗りつぶす e.Graphics.FillRectangle(New SolidBrush(Me.DataGridView1.ColumnHeadersDefaultCellStyle.BackColor), r1) Dim format As New StringFormat() format.Alignment = StringAlignment.Center format.LineAlignment = StringAlignment.Center ' テキストを書き込む e.Graphics.DrawString(Me.DataGridView1.Columns(leftIndex).HeaderText, Me.DataGridView1.ColumnHeadersDefaultCellStyle.Font, New SolidBrush(Me.DataGridView1.ColumnHeadersDefaultCellStyle.ForeColor), r1, format) Next Catch ex As Exception End Try End Sub
参考文献
メモし忘れた