⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datagridviewradiobuttoncell.cs

📁 vs.net2005 中DataGridView中加入RadioButton的源码,把多个RadioButton加入DataGridView的每一行中.
💻 CS
📖 第 1 页 / 共 5 页
字号:
                    Rectangle rectArrow = new Rectangle(scrollBounds.Right - this.layout.ScrollButtonsSize.Width,
                                                        scrollBounds.Top,
                                                        this.layout.ScrollButtonsSize.Width,
                                                        this.layout.ScrollButtonsSize.Height);
                    this.layout.UpButtonLocation = rectArrow.Location;
                    if (paint && DataGridViewRadioButtonCell.PartPainted(paintParts, DataGridViewPaintParts.ContentBackground))
                    {
                        ScrollBarRenderer.DrawArrowButton(graphics, rectArrow, GetScrollBarArrowButtonState(true, mouseOverCell ? mouseLocationCode : DATAGRIDVIEWRADIOBUTTONCELL_mouseLocationGeneric, this.layout.FirstDisplayedItemIndex > 0 /*enabled*/));
                    }
                    rectArrow.Y = scrollBounds.Bottom - this.layout.ScrollButtonsSize.Height;
                    this.layout.DownButtonLocation = rectArrow.Location;
                    if (paint && DataGridViewRadioButtonCell.PartPainted(paintParts, DataGridViewPaintParts.ContentBackground))
                    {
                        ScrollBarRenderer.DrawArrowButton(graphics, rectArrow, GetScrollBarArrowButtonState(false, mouseOverCell ? mouseLocationCode : DATAGRIDVIEWRADIOBUTTONCELL_mouseLocationGeneric, this.layout.FirstDisplayedItemIndex + this.layout.TotallyDisplayedItemsCount < this.Items.Count /*enabled*/));
                    }
                }

                // Finally paint the potential error icon
                if (paint && 
                    DataGridViewRadioButtonCell.PartPainted(paintParts, DataGridViewPaintParts.ErrorIcon) &&
                    !(cellCurrent && this.DataGridView.IsCurrentCellInEditMode) && 
                    this.DataGridView.ShowCellErrors)
                {
                    PaintErrorIcon(graphics, clipBounds, errorBounds, errorText);
                }
            }
            finally
            {
                if (backgroundBrush != null)
                {
                    backgroundBrush.Dispose();
                }
            }
        }

        /// <summary>
        /// Returns whether calling the OnContentClick method would force the owning row to be unshared.
        /// </summary>
        protected override bool ContentClickUnsharesRow(DataGridViewCellEventArgs e)
        {
            Point ptCurrentCell = this.DataGridView.CurrentCellAddress;
            return ptCurrentCell.X == this.ColumnIndex &&
                   ptCurrentCell.Y == e.RowIndex &&
                   this.DataGridView.IsCurrentCellInEditMode;
        }

        /// <summary>
        /// Raised when the owning grid gets a MouseUp notification
        /// </summary>
        private void DataGridView_MouseUp(object sender, MouseEventArgs e)
        {
            // Unhook the event handler
            this.DataGridView.MouseUp -= new MouseEventHandler(DataGridView_MouseUp);
            this.mouseUpHooked = false;
            // Reset the pressed item index. Since the mouse was released, no item can be pressed anymore.
            this.pressedItemIndex = -1;
        }

        /// <summary>
        /// Raised when the cell's DataSource is initialized.
        /// </summary>
        private void DataSource_Initialized(object sender, EventArgs e)
        {
            Debug.Assert(sender == this.DataSource);
            Debug.Assert(this.DataSource is ISupportInitializeNotification);
            Debug.Assert(this.dataSourceInitializedHookedUp);

            ISupportInitializeNotification dsInit = this.DataSource as ISupportInitializeNotification;
            // Unhook the Initialized event.
            if (dsInit != null)
            {
                dsInit.Initialized -= new EventHandler(DataSource_Initialized);
            }

            // The wait is over: the DataSource is initialized.
            this.dataSourceInitializedHookedUp = false;

            // Check the DisplayMember and ValueMember values - will throw if values don't match existing fields.
            InitializeDisplayMemberPropertyDescriptor(this.DisplayMember);
            InitializeValueMemberPropertyDescriptor(this.ValueMember);
        }

        /// <summary>
        /// Returns whether calling the OnEnter method would force the owning row to be unshared.
        /// </summary>
        protected override bool EnterUnsharesRow(int rowIndex, bool throughMouseClick)
        {
            return this.focusedItemIndex == -1;
        }

        /// <summary>
        /// Custom implementation of the GetContentBounds method which delegates most of the work to the ComputeLayout function.
        /// </summary>
        protected override Rectangle GetContentBounds(Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
        {
            if (this.DataGridView == null || rowIndex < 0 || this.OwningColumn == null)
            {
                return Rectangle.Empty;
            }

            // First determine the effective border style of this cell.
            bool singleVerticalBorderAdded = !this.DataGridView.RowHeadersVisible && this.DataGridView.AdvancedCellBorderStyle.All == DataGridViewAdvancedCellBorderStyle.Single;
            bool singleHorizontalBorderAdded = !this.DataGridView.ColumnHeadersVisible && this.DataGridView.AdvancedCellBorderStyle.All == DataGridViewAdvancedCellBorderStyle.Single;
            DataGridViewAdvancedBorderStyle dataGridViewAdvancedBorderStylePlaceholder = new DataGridViewAdvancedBorderStyle();

            Debug.Assert(rowIndex > -1 && this.OwningColumn != null);

            DataGridViewAdvancedBorderStyle dgvabsEffective = AdjustCellBorderStyle(this.DataGridView.AdvancedCellBorderStyle,
                dataGridViewAdvancedBorderStylePlaceholder,
                singleVerticalBorderAdded,
                singleHorizontalBorderAdded,
                rowIndex == this.DataGridView.Rows.GetFirstRow(DataGridViewElementStates.Displayed) /*isFirstDisplayedRow*/,
                this.ColumnIndex == this.DataGridView.Columns.GetFirstColumn(DataGridViewElementStates.Displayed).Index /*isFirstDisplayedColumn*/);

            // Next determine the state of this cell.
            DataGridViewElementStates rowState = this.DataGridView.Rows.GetRowState(rowIndex);
            DataGridViewElementStates cellState = CellStateFromColumnRowStates(rowState);
            cellState |= this.State;

            // Then the bounds of this cell.
            Rectangle cellBounds = new Rectangle(new Point(0, 0), GetSize(rowIndex));

            // Finally compute the layout of the cell and return the resulting content bounds.
            ComputeLayout(graphics,
                cellBounds,
                cellBounds,
                rowIndex,
                cellState,
                null /*formattedValue*/,            // contentBounds is independent of formattedValue
                null /*errorText*/,                 // contentBounds is independent of errorText
                cellStyle,
                dgvabsEffective,
                DataGridViewPaintParts.ContentForeground,
                false /*paint*/);

            return this.layout.ContentBounds;
        }

        /// <summary>
        /// Utility function that converts a constraintSize provided to GetPreferredSize into a 
        /// DataGridViewRadioButtonFreeDimension enum value.
        /// </summary>
        private static DataGridViewRadioButtonFreeDimension GetFreeDimensionFromConstraint(Size constraintSize)
        {
            if (constraintSize.Width < 0 || constraintSize.Height < 0)
            {
                throw new ArgumentException("InvalidArgument=Value of '" + constraintSize.ToString() + "' is not valid for 'constraintSize'.");
            }
            if (constraintSize.Width == 0)
            {
                if (constraintSize.Height == 0)
                {
                    return DataGridViewRadioButtonFreeDimension.Both;
                }
                else
                {
                    return DataGridViewRadioButtonFreeDimension.Width;
                }
            }
            else
            {
                if (constraintSize.Height == 0)
                {
                    return DataGridViewRadioButtonFreeDimension.Height;
                }
                else
                {
                    throw new ArgumentException("InvalidArgument=Value of '" + constraintSize.ToString() + "' is not valid for 'constraintSize'.");
                }
            }
        }

        /// <summary>
        /// Utility function that returns the display value of an item given the 
        /// display/value property descriptors and display/value property names.
        /// </summary>
        private object GetItemDisplayValue(object item)
        {
            Debug.Assert(item != null);
            bool displayValueSet = false;
            object displayValue = null;
            if (this.displayMemberProperty != null)
            {
                displayValue = this.displayMemberProperty.GetValue(item);
                displayValueSet = true;
            }
            else if (this.valueMemberProperty != null)
            {
                displayValue = this.valueMemberProperty.GetValue(item);
                displayValueSet = true;
            }
            else if (!string.IsNullOrEmpty(this.DisplayMember))
            {
                PropertyDescriptor propDesc = TypeDescriptor.GetProperties(item).Find(this.DisplayMember, true /*caseInsensitive*/);
                if (propDesc != null)
                {
                    displayValue = propDesc.GetValue(item);
                    displayValueSet = true;
                }
            }
            else if (!string.IsNullOrEmpty(this.ValueMember))
            {
                PropertyDescriptor propDesc = TypeDescriptor.GetProperties(item).Find(this.ValueMember, true /*caseInsensitive*/);
                if (propDesc != null)
                {
                    displayValue = propDesc.GetValue(item);
                    displayValueSet = true;
                }
            }
            if (!displayValueSet)
            {
                displayValue = item;
            }
            return displayValue;
        }

        /// <summary>
        /// Utility function that returns the value of an item given the 
        /// display/value property descriptors and display/value property names.
        /// </summary>
        private object GetItemValue(object item)
        {
            bool valueSet = false;
            object value = null;
            if (this.valueMemberProperty != null)
            {
                value = this.valueMemberProperty.GetValue(item);
                valueSet = true;
            }
            else if (this.displayMemberProperty != null)
            {
                value = this.displayMemberProperty.GetValue(item);
                valueSet = true;
            }
            else if (!string.IsNullOrEmpty(this.ValueMember))
            {
                PropertyDescriptor propDesc = TypeDescriptor.GetProperties(item).Find(this.ValueMember, true /*caseInsensitive*/);
                if (propDesc != null)
                {
                    value = propDesc.GetValue(item);
                    valueSet = true;
                }
            }
            if (!valueSet && !string.IsNullOrEmpty(this.DisplayMember))
            {
                PropertyDescriptor propDesc = TypeDescriptor.GetProperties(item).Find(this.DisplayMember, true /*caseInsensitive*/);
                if (propDesc != null)
                {
                    value = propDesc.GetValue(item);
                    valueSet = true;
                }
            }
            if (!valueSet)
            {
                value = item;
            }
            return value;
        }

        /// <summary>
        /// Returns the code identifying the part of the cell which is underneath the mouse pointer.
        /// </summary>
        private int GetMouseLocationCode(Graphics graphics, int rowIndex, DataGridViewCellStyle cellStyle, int mouseX, int mouseY)
        {
            // First determine this cell's effective border style.
            bool singleVerticalBorderAdded = !this.DataGridView.RowHeadersVisible && this.DataGridView.AdvancedCellBorderStyle.All == DataGridViewAdvancedCellBorderStyle.Single;
            bool singleHorizontalBorderAdded = !this.DataGridView.ColumnHeadersVisible && this.DataGridView.AdvancedCellBorderStyle.All == DataGridViewAdvancedCellBorderStyle.Single;
            bool isFirstDisplayedColumn = this.ColumnIndex == this.DataGridView.Columns.GetFirstColumn(DataGridViewElementStates.Displayed).Index;
            bool isFirstDisplayedRow = rowIndex == this.DataGridView.Rows.GetFirstRow(DataGridViewElementStates.Displayed);
            DataGridViewAdvancedBorderStyle dataGridViewAdvancedBorderStylePlaceholder = new DataGridViewAdvancedBorderStyle(), dataGridViewAdvancedBorderStyleEffective;
            dataGridViewAdvancedBorderStyleEffective = AdjustCellBorderStyle(this.DataGridView.AdvancedCellBorderStyle,
                                                                             dataGridViewAdvancedBorderStylePlaceholder,
                                                                             singleVerticalBorderAdded,
                                                                             singleHorizontalBorderAdded,
                                                                             isFirstDisplayedColumn,
                                                                             isFirstDisplayedRow);
            // Then its size.
            Rectangle cellBounds = this.DataGridView.GetCellDisplayRectangle(this.ColumnIndex, rowIndex, false /*cutOverflow*/);
            Debug.Assert(GetSize(rowIndex) == cellBounds.Size);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -