Following code worked for me. But I don't know why. Replace your Paint method with this:
protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
{
if (_ownerCell != null && _ownerCell.DataGridView == null)
{
_ownerCell = null;
}
if (DataGridView == null || (_ownerCell == null && _columnSpan == 1 && _rowSpan == 1))
{
base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
return;
}
var ownerCell = this;
var columnIndex = ColumnIndex;
var columnSpan = _columnSpan;
var rowSpan = _rowSpan;
if (_ownerCell != null)
{
ownerCell = _ownerCell;
columnIndex = _ownerCell.ColumnIndex;
rowIndex = _ownerCell.RowIndex;
columnSpan = _ownerCell.ColumnSpan;
rowSpan = _ownerCell.RowSpan;
value = _ownerCell.GetValue(rowIndex);
errorText = _ownerCell.GetErrorText(rowIndex);
cellState = _ownerCell.State;
cellStyle = _ownerCell.GetInheritedStyle(null, rowIndex, true);
formattedValue = _ownerCell.GetFormattedValue(value, rowIndex, ref cellStyle, null, null, DataGridViewDataErrorContexts.Display);
}
if (CellsRegionContainsSelectedCell(columnIndex, rowIndex, columnSpan, rowSpan))
{
cellState |= DataGridViewElementStates.Selected;
}
RectangleF oldBounds = graphics.ClipBounds;
var cellBounds2 = DataGridViewCellExHelper.GetSpannedCellBoundsFromChildCellBounds(this, cellBounds, DataGridView.SingleVerticalBorderAdded(), DataGridView.SingleHorizontalBorderAdded());
clipBounds = DataGridViewCellExHelper.GetSpannedCellClipBounds(ownerCell, cellBounds2, DataGridView.SingleVerticalBorderAdded(), DataGridView.SingleHorizontalBorderAdded());
advancedBorderStyle = DataGridViewCellExHelper.AdjustCellBorderStyle(ownerCell);
using (var g = this.DataGridView.CreateGraphics())
{
g.SetClip(clipBounds);
ownerCell.NativePaint(g, clipBounds, cellBounds2, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts & ~DataGridViewPaintParts.Border);
if ((paintParts & DataGridViewPaintParts.Border) != DataGridViewPaintParts.None)
{
var leftTopCell = ownerCell;
var advancedBorderStyle2 = new DataGridViewAdvancedBorderStyle
{
Left = advancedBorderStyle.Left,
Top = advancedBorderStyle.Top,
Right = DataGridViewAdvancedCellBorderStyle.None,
Bottom = DataGridViewAdvancedCellBorderStyle.None
};
leftTopCell.PaintBorder(g, clipBounds, cellBounds2, cellStyle, advancedBorderStyle2);
var rightBottomCell = DataGridView[columnIndex + columnSpan - 1, rowIndex + rowSpan - 1] as DataGridViewSpannedTextBoxCell ?? this;
var advancedBorderStyle3 = new DataGridViewAdvancedBorderStyle
{
Left = DataGridViewAdvancedCellBorderStyle.None,
Top = DataGridViewAdvancedCellBorderStyle.None,
Right = advancedBorderStyle.Right,
Bottom = advancedBorderStyle.Bottom
};
rightBottomCell.PaintBorder(g, clipBounds, cellBounds2, cellStyle, advancedBorderStyle3);
}
}
graphics.SetClip(clipBounds);
ownerCell.NativePaint(graphics, clipBounds, cellBounds2, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts & ~DataGridViewPaintParts.Border);
if ((paintParts & DataGridViewPaintParts.Border) != DataGridViewPaintParts.None)
{
var leftTopCell = ownerCell;
var advancedBorderStyle2 = new DataGridViewAdvancedBorderStyle
{
Left = advancedBorderStyle.Left,
Top = advancedBorderStyle.Top,
Right = DataGridViewAdvancedCellBorderStyle.None,
Bottom = DataGridViewAdvancedCellBorderStyle.None
};
leftTopCell.PaintBorder(graphics, clipBounds, cellBounds2, cellStyle, advancedBorderStyle2);
var rightBottomCell = DataGridView[columnIndex + columnSpan - 1, rowIndex + rowSpan - 1] as DataGridViewSpannedTextBoxCell ?? this;
var advancedBorderStyle3 = new DataGridViewAdvancedBorderStyle
{
Left = DataGridViewAdvancedCellBorderStyle.None,
Top = DataGridViewAdvancedCellBorderStyle.None,
Right = advancedBorderStyle.Right,
Bottom = advancedBorderStyle.Bottom
};
rightBottomCell.PaintBorder(graphics, clipBounds, cellBounds2, cellStyle, advancedBorderStyle3);
}
graphics.SetClip(oldBounds);
}
|