Hallo,
mit folgendem Code den ich mir hier mal raukopiert habe verbinde ich Zellen im DGVProtected 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)
'Stop
Try
Dim mergeindex As Integer = ColumnIndex - m_nLeftColumn
Dim i As Integer
Dim nWidth As Integer
Dim nWidthLeft As Integer
Dim strText As String
Dim pen As New Pen(Brushes.Black)
' Draw the background
graphics.FillRectangle(New SolidBrush(System.Drawing.Color.White), _
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 New StringFormat()
sf.Alignment = StringAlignment.Near
sf.LineAlignment = StringAlignment.Near
sf.Trimming = StringTrimming.EllipsisCharacter
' Determine the total width of the merged cell
nWidth = 0
For i = m_nLeftColumn To m_nRightColumn
nWidth += Me.OwningRow.Cells(i).Size.Width
Next
' Determine the width before the current cell.
nWidthLeft = 0
For i = m_nLeftColumn To ColumnIndex - 1
nWidthLeft += Me.OwningRow.Cells(i).Size.Width
Next
' Retrieve the text to be displayed
strText = Me.OwningRow.Cells(m_nLeftColumn).FormattedValue.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())
Trace.WriteLine(Err.Description)
End Try
End Sub Das ist nur ein Teil aus der entsprechenden Class. Das funktioniert so weit recht gut.
Jetzt geht es mir noch um das "Feintuning".
Bei einer Aktualisierung der Daten in der Datenquelle, z.B. rückspeichern in die DB wird diese Ansicht überschrieben. Ich müsste jetzt alle sichtbaren Zeilen neu zeichnen.
In welchem Event sollte ich das machen?
Wenn ich mit den Pfeiltasten oder Tab duch das DGV klicke, wird immer die aktuelle Zelle blau markiert. Aber nicht bei diesen verbundenen Zellen. Wie könnte ich das lösen, das auch diese entsprechend markiert werden?
Und zuletzt, wenn ich eine verbundene Zelle zum bearbeiten auswähle (F2 oder Doppelklick), habe ich zur Bearbeitung nur den Bereich der ursprünglichen Zelle verfügbar. Wie kann man das auf die komplette verbundene Größe ausdehnen? Ich hatte schon versucht eine Textbox darüberzulegen, aber die kam anscheinend nicht in den Vordergrund, das hatte nicht geklappt.
Gruß Christian |