Ciao a tutti. Ho implementato un sistema per fondere le celle con lo stesso valore su una riga. Bene. Unico problema: quando seleziono una cella, mi copre parzialmente il testo sulla cella selezionata. Come posso evitarlo? Ecco il codice:
codice:
Private Sub Griglia_CellPainting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles Griglia.CellPainting
If e.ColumnIndex > -1 AndAlso e.RowIndex > -1 AndAlso Not IsNothing(e.Value) Then
'Merging delle celle con lo stesso valore
Dim isLast As Boolean = False
Using gridBrush As Brush = New SolidBrush(Griglia.GridColor), backColorBrush As Brush _
= New SolidBrush(e.CellStyle.BackColor)
Using gridLinePen As Pen = New Pen(gridBrush)
' Pulisce cella
If Not (e.State = 97) Then _
e.Graphics.FillRectangle(backColorBrush, e.CellBounds)
'tiro bordi inferiore e superiore
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, _
e.CellBounds.Right - 1, e.CellBounds.Bottom - 1)
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Top - 1, _
e.CellBounds.Right - 1, e.CellBounds.Top - 1)
If (e.ColumnIndex > 0 And e.ColumnIndex < Griglia.ColumnCount - 2) AndAlso _
Griglia.Item(e.ColumnIndex - 1, e.RowIndex).Value <> e.Value Then
'Se è la prima, tiro pure il bordo sinistro
iMergeFirst = e.ColumnIndex
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left - 1, e.CellBounds.Top, e.CellBounds.Left - 1, _
e.CellBounds.Bottom)
End If
If e.ColumnIndex < Griglia.ColumnCount - 2 AndAlso Griglia.Item(e.ColumnIndex + 1, e.RowIndex).Value <> _
e.Value Then
'Se è l'ultima, tiro pure il bordo destro
isLast = True
iMergeLast = e.ColumnIndex
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, _
e.CellBounds.Bottom)
End If
' Merging del testo delle colonne
If Not e.Value Is Nothing AndAlso isLast AndAlso (iMergeFirst > 0 And iMergeLast > 0) Then
' If e.RowIndex > 0 AndAlso Griglia.Rows(e.RowIndex - 1).Cells(e.ColumnIndex).Value.ToString() = e.Value.ToString() Then
' Else
'Stimo la larghezza del rettangolo che conterrà la stringa
Dim StringWidth As Single = 0
For i As Integer = iMergeFirst To iMergeLast
StringWidth += Griglia.GetCellDisplayRectangle(i, e.RowIndex, False).Width
Next
Dim StringCell As New Rectangle(Griglia.GetCellDisplayRectangle(iMergeFirst, e.RowIndex, False).X, _
Griglia.GetCellDisplayRectangle(iMergeFirst, e.RowIndex, False).Y, _
StringWidth, e.CellBounds.Height)
e.Graphics.DrawString(CType(e.Value, String), e.CellStyle.Font, Brushes.Black, _
StringCell.X, _
StringCell.Y, StringFormat.GenericDefault)
'end if
End If
e.Handled = True
End Using
End Using
End If
End Sub