1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| Public Class HMergedCell
Inherits DataGridViewTextBoxCell
Private m_nLeftColumn As Integer = 0
Private m_nRightColumn As Integer = 0
''' <summary>
''' Column Index of the left-most cell to be merged.
''' This cell controls the merged text.
''' </summary>
''' <remarks></remarks>
Public Property LeftColumn() As Integer
Get
Return m_nLeftColumn
End Get
Set(ByVal value As Integer)
m_nLeftColumn = value
End Set
End Property
''' <summary>
''' Column Index of the right-most cell to be merged
''' </summary>
''' <remarks></remarks>
Public Property RightColumn() As Integer
Get
Return m_nRightColumn
End Get
Set(ByVal value As Integer)
m_nRightColumn = value
End Set
End Property
Protected Overrides Sub Paint(ByVal graphics As Graphics, ByVal clipBounds As Rectangle, ByVal cellBounds As Rectangle, ByVal rowIndex As Integer, ByVal cellState As DataGridViewElementStates, _
ByVal value As Object, ByVal formattedValue As Object, ByVal errorText As String, ByVal cellStyle As DataGridViewCellStyle, ByVal advancedBorderStyle As DataGridViewAdvancedBorderStyle, _
ByVal paintParts As DataGridViewPaintParts)
Try
Dim mergeindex As Integer = ColumnIndex - m_nLeftColumn
Dim i As Integer = 0
Dim nWidth As Integer = 0
Dim nWidthLeft As Integer = 0
Dim strText As String = Nothing
Dim pen As Pen = New Pen(Brushes.Black)
' Draw the background
graphics.FillRectangle(New SolidBrush(SystemColors.Control), cellBounds)
' Draw the separator for rows
graphics.DrawLine(New Pen(New SolidBrush(SystemColors.ControlDark)), cellBounds.Left, cellBounds.Bottom - 1, cellBounds.Right, _
cellBounds.Bottom - 1)
' Draw the right vertical line for the cell
If (ColumnIndex = m_nRightColumn) Then
graphics.DrawLine(New Pen(New SolidBrush(SystemColors.ControlDark)), cellBounds.Right - 1, cellBounds.Top, cellBounds.Right - 1, _
cellBounds.Bottom)
End If
' Draw the text
Dim rectDest As RectangleF = RectangleF.Empty
Dim sf As StringFormat = New StringFormat()
sf.Alignment = StringAlignment.Center
sf.LineAlignment = StringAlignment.Center
sf.Trimming = StringTrimming.EllipsisCharacter
' Determine the total width of the merged cell
nWidth = 0
i = m_nLeftColumn
Do Until i <= m_nRightColumn
nWidth += Me.OwningRow.Cells(i).Size.Width
i += 1
Loop
' Determine the width before the current cell.
nWidthLeft = 0
i = m_nLeftColumn
Do Until i <= ColumnIndex - 1
nWidthLeft += Me.OwningRow.Cells(i).Size.Width
i += 1
Loop
' Retrieve the text to be displayed
strText = Me.OwningRow.Cells(m_nLeftColumn).Value.ToString()
rectDest = New RectangleF(cellBounds.Left - nWidthLeft, cellBounds.Top, nWidth, cellBounds.Height)
graphics.DrawString(strText, New Font("Arial", 10, FontStyle.Regular), Brushes.Black, rectDest, sf)
Catch ex As Exception
Trace.WriteLine(ex.ToString())
End Try
End Sub
End Class |
Partager