コード例 #1
0
        public override System.Drawing.Rectangle PositionEditingPanel(System.Drawing.Rectangle cellBounds, System.Drawing.Rectangle cellClip, DataGridViewCellStyle cellStyle, bool singleVerticalBorderAdded, bool singleHorizontalBorderAdded, bool isFirstDisplayedColumn, bool isFirstDisplayedRow)
        {
            if (ownerCell == null && columnSpan == 1 && rowSpan == 1)
            {
                return(base.PositionEditingPanel(cellBounds, cellClip, cellStyle, singleVerticalBorderAdded, singleHorizontalBorderAdded, isFirstDisplayedColumn, isFirstDisplayedRow));
            }

            var _ownerCell = this;

            if (ownerCell != null)
            {
                var rowIndex = ownerCell.RowIndex;
                cellStyle = ownerCell.GetInheritedStyle(null, rowIndex, true);
                ownerCell.GetFormattedValue(ownerCell.Value, rowIndex, ref cellStyle, null, null, DataGridViewDataErrorContexts.Formatting);
                var editingControl = DataGridView.EditingControl as IDataGridViewEditingControl;
                if (editingControl != null)
                {
                    editingControl.ApplyCellStyleToEditingControl(cellStyle);
                    var editingPanel = DataGridView.EditingControl.Parent;
                    if (editingPanel != null)
                    {
                        editingPanel.BackColor = cellStyle.BackColor;
                    }
                }
                _ownerCell = ownerCell;
            }
            cellBounds = DataGridViewCellExHelper.GetSpannedCellBoundsFromChildCellBounds(this, cellBounds, singleVerticalBorderAdded, singleHorizontalBorderAdded);
            cellClip   = DataGridViewCellExHelper.GetSpannedCellClipBounds(_ownerCell, cellBounds, singleVerticalBorderAdded, singleHorizontalBorderAdded);

            return(base.PositionEditingPanel(cellBounds
                                             , cellClip
                                             , cellStyle
                                             , singleVerticalBorderAdded
                                             , singleHorizontalBorderAdded
                                             , DataGridViewCellExHelper.InFirstDisplayedColumn(_ownerCell)
                                             , DataGridViewCellExHelper.InFirstDisplayedRow(_ownerCell)));
        }
コード例 #2
0
        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;
            }

            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;
            }

            var cellBounds2 = DataGridViewCellExHelper.GetSpannedCellBoundsFromChildCellBounds
                                  (this
                                  , cellBounds
                                  , DataGridViewCellExHelper.SingleVerticalBorderAdded(DataGridView)
                                  , DataGridViewCellExHelper.SingleHorizontalBorderAdded(DataGridView));

            clipBounds = DataGridViewCellExHelper.GetSpannedCellClipBounds
                             (_ownerCell
                             , cellBounds2
                             , DataGridViewCellExHelper.SingleVerticalBorderAdded(DataGridView)
                             , DataGridViewCellExHelper.SingleHorizontalBorderAdded(DataGridView));

            using (var g = DataGridView.CreateGraphics())
            {
                g.SetClip(clipBounds);

                advancedBorderStyle = DataGridViewCellExHelper.AdjustCellBorderStyle(_ownerCell);

                _ownerCell.NativePaint(g, clipBounds, cellBounds2, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts & ~DataGridViewPaintParts.Border);

                if ((paintParts & DataGridViewPaintParts.Border) != DataGridViewPaintParts.None)          //这表示是合并单元格的最左边的单元格,边框不能画四边,只能画左上2边
                {
                    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 DataGridViewTextBoxCellEx ?? this;

                    var advancedBorderStyle3 = new DataGridViewAdvancedBorderStyle
                    {
                        Left   = DataGridViewAdvancedCellBorderStyle.None,
                        Top    = DataGridViewAdvancedCellBorderStyle.None,
                        Right  = advancedBorderStyle.Right,
                        Bottom = advancedBorderStyle.Bottom
                    };
                    rightBottomCell.PaintBorder(g, clipBounds, cellBounds2, cellStyle, advancedBorderStyle3);
                }
            }
        }