protected override void Paint(System.Drawing.Graphics graphics, System.Drawing.Rectangle clipBounds, System.Drawing.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; //owner cell was removed.
            }
            if (this.DataGridView == null || (_ownerCell == null && _columnSpan == 1 && _rowSpan == 1))
            {
                base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
                return;
            }

            ExDataGridViewTextBoxCell ownerCell = this;
            int columnIndex = this.ColumnIndex;
            int columnSpan  = _columnSpan;
            int 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;
            }

            cellBounds = GetSpannedCellBoundsFromChildCellBounds(this, cellBounds);
            clipBounds = GetSpannedCellClipBounds(ownerCell, cellBounds);

            using (Graphics g = this.DataGridView.CreateGraphics())
            {
                g.SetClip(clipBounds);
                //Paint the content.
                ownerCell.InternalPaint(g, clipBounds, cellBounds, rowIndex, cellState,
                                        value, formattedValue, errorText,
                                        cellStyle, advancedBorderStyle,
                                        paintParts & ~DataGridViewPaintParts.Border);
                //Paint the borders.
                if ((paintParts & DataGridViewPaintParts.Border) != DataGridViewPaintParts.None)
                {
                    ExDataGridViewTextBoxCell       leftTopCell          = ownerCell;
                    DataGridViewAdvancedBorderStyle advancedBorderStyle2 = new DataGridViewAdvancedBorderStyle();
                    advancedBorderStyle2.Left   = advancedBorderStyle.Left;
                    advancedBorderStyle2.Top    = advancedBorderStyle.Top;
                    advancedBorderStyle2.Right  = DataGridViewAdvancedCellBorderStyle.None;
                    advancedBorderStyle2.Bottom = DataGridViewAdvancedCellBorderStyle.None;

                    leftTopCell.PaintBorder(g, clipBounds, cellBounds, cellStyle, advancedBorderStyle2);

                    ExDataGridViewTextBoxCell rightBottomCell = DataGridView[columnIndex + columnSpan - 1, rowIndex + rowSpan - 1] as ExDataGridViewTextBoxCell;
                    if (rightBottomCell == null)
                    {
                        rightBottomCell = this;
                    }

                    DataGridViewAdvancedBorderStyle advancedBorderStyle3 = new DataGridViewAdvancedBorderStyle();
                    advancedBorderStyle3.Left   = DataGridViewAdvancedCellBorderStyle.None;
                    advancedBorderStyle3.Top    = DataGridViewAdvancedCellBorderStyle.None;
                    advancedBorderStyle3.Right  = advancedBorderStyle.Right;
                    advancedBorderStyle3.Bottom = advancedBorderStyle.Bottom;

                    rightBottomCell.PaintBorder(g, clipBounds, cellBounds, cellStyle, advancedBorderStyle3);
                }
            }
        }