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

📄 datagridviewradiobuttoncell.cs

📁 vs.net2005 中DataGridView中加入RadioButton的源码,把多个RadioButton加入DataGridView的每一行中.
💻 CS
📖 第 1 页 / 共 5 页
字号:
            {
                if (String.IsNullOrEmpty(valueMember))
                {
                    this.valueMemberProperty = null;
                }
                else
                {
                    BindingMemberInfo valueBindingMember = new BindingMemberInfo(valueMember);
                    // make the DataManager point to the sublist inside this.DataSource
                    this.DataManager = this.DataGridView.BindingContext[this.DataSource, valueBindingMember.BindingPath] as CurrencyManager;

                    PropertyDescriptorCollection props = this.DataManager.GetItemProperties();
                    PropertyDescriptor valueMemberProperty = props.Find(valueBindingMember.BindingField, true);
                    if (valueMemberProperty == null)
                    {
                        throw new ArgumentException("Field called '" + valueMember + "' does not exist.");
                    }
                    else
                    {
                        this.valueMemberProperty = valueMemberProperty;
                    }
                }
            }
        }

        /// <summary>
        /// Helper function that invalidates the entire area of an entry
        /// </summary>
        private void InvalidateItem(int itemIndex, int rowIndex)
        {
            if (itemIndex >= this.layout.FirstDisplayedItemIndex &&
                itemIndex < this.layout.FirstDisplayedItemIndex + this.layout.DisplayedItemsCount)
            {
                DataGridViewCellStyle cellStyle = GetInheritedStyle(null, rowIndex, false /* includeColors */);
                Point radioButtonLocation = this.layout.FirstDisplayedItemLocation;
                int textHeight = cellStyle.Font.Height;
                radioButtonLocation.Y += (textHeight + DATAGRIDVIEWRADIOBUTTONCELL_margin) * (itemIndex - this.layout.FirstDisplayedItemIndex);
                Size cellSize = GetSize(rowIndex);
                this.DataGridView.Invalidate(new Rectangle(radioButtonLocation.X, radioButtonLocation.Y, cellSize.Width, Math.Max(textHeight + DATAGRIDVIEWRADIOBUTTONCELL_margin, this.layout.RadioButtonsSize.Height)));
            }
        }

        /// <summary>
        /// Helper function that invalidates the glyph section of an entry
        /// </summary>
        private void InvalidateRadioGlyph(int itemIndex, DataGridViewCellStyle cellStyle)
        {
            if (itemIndex >= this.layout.FirstDisplayedItemIndex &&
                itemIndex < this.layout.FirstDisplayedItemIndex + this.layout.DisplayedItemsCount)
            {
                Point radioButtonLocation = this.layout.FirstDisplayedItemLocation;
                int textHeight = cellStyle.Font.Height;
                radioButtonLocation.Y += (textHeight + DATAGRIDVIEWRADIOBUTTONCELL_margin) * (itemIndex - this.layout.FirstDisplayedItemIndex);
                this.DataGridView.Invalidate(new Rectangle(radioButtonLocation, this.layout.RadioButtonsSize));
            }
        }

        /// <summary>
        /// Returns whether calling the OnKeyDown method would force the owning row to be unshared.
        /// </summary>
        protected override bool KeyDownUnsharesRow(KeyEventArgs e, int rowIndex)
        {
            if (!e.Alt && !e.Control && !e.Shift)
            {
                if (this.handledKeyDown)
                {
                    return true;
                }
                if (e.KeyCode == Keys.Down && this.focusedItemIndex < this.Items.Count - 1)
                {
                    return true;
                }
                else if (e.KeyCode == Keys.Up && this.focusedItemIndex > 0)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// Returns whether calling the OnKeyUp method would force the owning row to be unshared.
        /// </summary>
        protected override bool KeyUpUnsharesRow(KeyEventArgs e, int rowIndex)
        {
            if (!e.Alt && !e.Control && !e.Shift)
            {
                if (e.KeyCode == Keys.Down && this.focusedItemIndex < this.Items.Count - 1 && this.handledKeyDown)
                {
                    return true;
                }
                else if (e.KeyCode == Keys.Up && this.focusedItemIndex > 0 && this.handledKeyDown)
                {
                    return true;
                }
            }
            if (this.handledKeyDown)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// Returns whether calling the OnMouseDown method would force the owning row to be unshared.
        /// </summary>
        protected override bool MouseDownUnsharesRow(DataGridViewCellMouseEventArgs e)
        {
            if (this.DataGridView == null)
            {
                return false;
            }
            if (e.Button == MouseButtons.Left)
            {
                int mouseLocationCode = GetMouseLocationCode(this.DataGridView.CreateGraphics(),
                                                             e.RowIndex,
                                                             GetInheritedStyle(null, e.RowIndex, false /* includeColors */),
                                                             e.X,
                                                             e.Y);
                switch (mouseLocationCode)
                {
                    case DATAGRIDVIEWRADIOBUTTONCELL_mouseLocationGeneric:
                        break;
                    case DATAGRIDVIEWRADIOBUTTONCELL_mouseLocationBottomScrollButton:
                        if (this.layout.FirstDisplayedItemIndex + this.layout.DisplayedItemsCount < this.Items.Count)
                        {
                            return true;
                        }
                        break;
                    case DATAGRIDVIEWRADIOBUTTONCELL_mouseLocationTopScrollButton:
                        if (this.layout.FirstDisplayedItemIndex > 0)
                        {
                            return true;
                        }
                        break;
                    default:
                        if (this.pressedItemIndex != mouseLocationCode + this.layout.FirstDisplayedItemIndex)
                        {
                            return true;
                        }
                        break;
                }
            }
            return false;
        }

        /// <summary>
        /// Returns whether calling the OnMouseLeave method would force the owning row to be unshared.
        /// </summary>
        protected override bool MouseLeaveUnsharesRow(int rowIndex)
        {
            return this.pressedItemIndex != -1 && !this.mouseUpHooked;
        }

        /// <summary>
        /// Returns whether calling the OnMouseUp method would force the owning row to be unshared.
        /// </summary>
        protected override bool MouseUpUnsharesRow(DataGridViewCellMouseEventArgs e)
        {
             return e.Button == MouseButtons.Left && this.pressedItemIndex != -1;
        }

        /// <summary>
        /// Method that declares the cell dirty and notifies the grid of the value change.
        /// </summary>
        private void NotifyDataGridViewOfValueChange()
        {
            this.valueChanged = true;
            Debug.Assert(this.DataGridView != null);
            this.DataGridView.NotifyCurrentCellDirty(true);
        }

        /// <summary>
        /// Potentially updates the selected item and notifies the grid of the change.
        /// </summary>
        protected override void OnContentClick(DataGridViewCellEventArgs e)
        {
            if (this.DataGridView == null)
            {
                return;
            }
            Point ptCurrentCell = this.DataGridView.CurrentCellAddress;
            if (ptCurrentCell.X == this.ColumnIndex &&
                ptCurrentCell.Y == e.RowIndex &&
                this.DataGridView.IsCurrentCellInEditMode)
            {
                if (mouseLocationCode >= 0 && 
                    UpdateFormattedValue(this.layout.FirstDisplayedItemIndex + mouseLocationCode, e.RowIndex))
                {
                    NotifyDataGridViewOfValueChange();
                }
            }
        }

        /// <summary>
        /// Updates the property descriptors when the cell gets attached to the grid.
        /// </summary>
        protected override void OnDataGridViewChanged()
        {
            if (this.DataGridView != null)
            {
                // Will throw an error if DataGridView is set and a member is invalid
                InitializeDisplayMemberPropertyDescriptor(this.DisplayMember);
                InitializeValueMemberPropertyDescriptor(this.ValueMember);
            }
            base.OnDataGridViewChanged();
        }

        /// <summary>
        /// Makes sure that there is a focused item when the cell becomes the current one.
        /// </summary>
        protected override void OnEnter(int rowIndex, bool throughMouseClick)
        {
            if (this.focusedItemIndex == -1)
            {
                this.focusedItemIndex = this.layout.FirstDisplayedItemIndex;
            }
            base.OnEnter(rowIndex, throughMouseClick);
        }

        /// <summary>
        /// Handles the KeyDown notification when it can result in a value change.
        /// </summary>
        protected override void OnKeyDown(KeyEventArgs e, int rowIndex)
        {
            if (this.DataGridView == null)
            {
                return;
            }
            if (!e.Alt && !e.Control && !e.Shift)
            {
                if (this.handledKeyDown)
                {
                    this.handledKeyDown = false;
                }
                if (e.KeyCode == Keys.Down && this.focusedItemIndex < this.Items.Count - 1)
                {
                    this.handledKeyDown = true;
                    e.Handled = true;
                }
                else if (e.KeyCode == Keys.Up && this.focusedItemIndex > 0)
                {
                    this.handledKeyDown = true;
                    e.Handled = true;
                }
            }
        }

        /// <summary>
        /// Handles the KeyUp notification to change the cell's value.
        /// </summary>
        protected override void OnKeyUp(KeyEventArgs e, int rowIndex)
        {
            if (this.DataGridView == null)
            {
                return;
            }
            if (!e.Alt && !e.Control && !e.Shift && this.handledKeyDown)
            {
                if (e.KeyCode == Keys.Down && this.focusedItemIndex < this.Items.Count - 1)
                {
                    // Handle the Down Arrow key
                    if (UpdateFormattedValue(this.focusedItemIndex+1, rowIndex))
                    {
                        NotifyDataGridViewOfValueChange();
                    }
                    else if (this.selectedItemIndex == this.focusedItemIndex+1)
                    {
                        this.focusedItemIndex++;
                    }
                    if (this.focusedItemIndex >= this.layout.FirstDisplayedItemIndex + this.layout.TotallyDisplayedItemsCount)
                    {
                        this.layout.FirstDisplayedItemIndex++;
                    }
                    while (this.focusedItemIndex < this.layout.FirstDisplayedItemIndex)
                    {
                        this.layout.FirstDisplayedItemIndex--;
                    }
                    this.DataGridView.InvalidateCell(this.ColumnIndex, rowIndex);
                

⌨️ 快捷键说明

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