C# DataGridViewRadioButtonColumn

单元格:

View Code
/// <summary>
    /// radio button单元格
    /// </summary>
    /// <remarks>绑定数据后不能在构造函数中修改属性</remarks>
    public class DataGridViewRadioButtonCell : DataGridViewTextBoxCell
    {
        Point radioButtonLocation;
        Size radioButtonSize;

        Point _cellLocation = new Point();

        System.Windows.Forms.VisualStyles.RadioButtonState _cbState =
            System.Windows.Forms.VisualStyles.RadioButtonState.UncheckedNormal;

        #region Property

        #region 行为
        private DataGridViewTriState _isReadOnly = DataGridViewTriState.NotSet;
        /// <summary>
        /// 是否只读
        /// </summary>
        [Category("行为")]
        [Description("是否只读")]
        [DefaultValue(DataGridViewTriState.NotSet)]
        [Browsable(false)]
        public DataGridViewTriState IsReadOnly
        {
            get
            {
                if (_isReadOnly == DataGridViewTriState.NotSet)
                {
                    if (this.OwningColumn != null && OwningColumn is DataGridViewRadioButtonColumn)
                    {
                        _isReadOnly = (this.OwningColumn as DataGridViewRadioButtonColumn).IsReadOnly ? DataGridViewTriState.True : DataGridViewTriState.False;
                    }
                }
                return _isReadOnly;
            }
            set
            {
                if (value == DataGridViewTriState.NotSet)
                {
                    value = DataGridViewTriState.False;
                }
                _isReadOnly = value;
            }
        }

        /// <summary>
        /// 不允许编辑
        /// </summary>
        [Browsable(false)]
        [DefaultValue(true)]
        public override bool ReadOnly
        {
            get { return true; }
            set
            {
                base.ReadOnly = true;
            }
        }

        private bool _isChecked = false;
        /// <summary>
        /// 是否选中
        /// </summary>
        [Category("行为")]
        [Description("是否选中")]
        [DefaultValue(false)]
        [RefreshProperties(RefreshProperties.Repaint)]
        public bool Checked
        {
            get { return _isChecked; }
            set
            {
                //重新界定值
                base.Value = value ? "1" : "0";
                if (_isChecked != value)
                {
                    _isChecked = value;
                    if (value
                        && this.OwningColumn != null
                        && this.DataGridView != null
                        && this.OwningColumn is DataGridViewRadioButtonColumn)
                    {
                        //单行选择
                        if ((this.OwningColumn as DataGridViewRadioButtonColumn).SingleOn)
                        {
                            foreach (DataGridViewRow dr in this.DataGridView.Rows)
                            {
                                if (dr.Index != this.RowIndex)
                                {
                                    (dr.Cells[this.ColumnIndex] as DataGridViewRadioButtonCell).Checked = false;
                                }
                            }
                        }
                    }
                    if (OnRadioButtonClicked != null)
                    {
                        OnRadioButtonClicked(this, Checked);
                    }
                    if (this.DataGridView != null)
                    {
                        this.DataGridView.InvalidateCell(this);
                    }
                }
            }
        }

        private bool _enabled = true;
        /// <summary>
        /// 是否可用
        /// </summary>
        [Category("行为")]
        [Description("是否可用")]
        [DefaultValue(true)]
        [RefreshProperties(RefreshProperties.Repaint)]
        public bool Enabled
        {
            get { return _enabled; }
            set
            {
                if (_enabled != value)
                {
                    _enabled = value;
                    if (this.DataGridView != null)
                    {
                        this.DataGridView.InvalidateCell(this);
                    }
                }
            }
        }
        #endregion

        /*
        /// <summary>
        /// 值
        /// </summary>
        [Category("数据")]
        [Description("值")]
        [DefaultValue(true)]
        [RefreshProperties(RefreshProperties.Repaint)]
        public new string Value
        {
            get
            {
                return (Checked ? "1" : "0");
            }
            set
            {
                if (value != null && value.Equals("1"))
                {
                    Checked = true;
                }
                else
                {
                    Checked = false;
                }
            }
        }
        */

        private bool _isDefault = false;
        /// <summary>
        /// 是否是默认行
        /// </summary>
        [Category("行为")]
        [Description("是否是默认行")]
        [DefaultValue(false)]
        [RefreshProperties(RefreshProperties.Repaint)]
        public bool IsDefault
        {
            get { return _isDefault; }
            set
            {
                if (_isDefault != value)
                {
                    _isDefault = value;
                }
            }
        }

        private bool _display = true;
        /// <summary>
        /// 是否可见
        /// </summary>
        [Category("外观")]
        [Description("是否可见")]
        [DefaultValue(true)]
        [RefreshProperties(RefreshProperties.Repaint)]
        public bool Display
        {
            get { return _display; }
            set
            {
                if (_display != value)
                {
                    _display = value;
                    if (this.DataGridView != null)
                    {
                        this.DataGridView.InvalidateCell(this);
                    }
                }
            }
        }

        public event RadioButtonClickedHandler OnRadioButtonClicked;

        /// <summary>
        /// 是否已绑定事件
        /// </summary>
        [DefaultValue(true)]
        [Browsable(false)]
        public bool EventIsNull
        {
            get
            {
                return OnRadioButtonClicked == null;
            }
        }

        #endregion

        public DataGridViewRadioButtonCell()
        {
            base.ReadOnly = true;
        }

        #region 绘制
        protected override void Paint(System.Drawing.Graphics graphics, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle cellBounds, int rowIndex, DataGridViewElementStates dataGridViewElementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
        {
            base.Paint(graphics, clipBounds, cellBounds, rowIndex, dataGridViewElementState, null, null, errorText, cellStyle, advancedBorderStyle, paintParts);
            if (Display)
            {
                Point p = new Point();
                Size s = RadioButtonRenderer.GetGlyphSize(graphics, System.Windows.Forms.VisualStyles.RadioButtonState.UncheckedNormal);
                p.X = cellBounds.Location.X + (cellBounds.Width / 2) - (s.Width / 2);
                p.Y = cellBounds.Location.Y + (cellBounds.Height / 2) - (s.Height / 2);
                _cellLocation = cellBounds.Location;
                radioButtonLocation = p;
                radioButtonSize = s;

                if (Checked)
                {
                    if (Enabled)
                    {
                        _cbState = System.Windows.Forms.VisualStyles.RadioButtonState.CheckedNormal;
                    }
                    else
                    {
                        _cbState = System.Windows.Forms.VisualStyles.RadioButtonState.CheckedDisabled;
                    }
                }
                else
                {
                    if (Enabled)
                    {
                        _cbState = System.Windows.Forms.VisualStyles.RadioButtonState.UncheckedNormal;
                    }
                    else
                    {
                        _cbState = System.Windows.Forms.VisualStyles.RadioButtonState.UncheckedDisabled;
                    }
                }
                RadioButtonRenderer.DrawRadioButton(graphics, radioButtonLocation, _cbState);
            }
        }

        protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
        {
            //可以编辑
            if (!(IsReadOnly == DataGridViewTriState.True)
                && Display
                && Enabled)
            {
                Point p = new Point(e.X + _cellLocation.X, e.Y + _cellLocation.Y);
                if (p.X >= radioButtonLocation.X
                    && p.X <= radioButtonLocation.X + radioButtonSize.Width
                    && p.Y >= radioButtonLocation.Y
                    && p.Y <= radioButtonLocation.Y + radioButtonSize.Height)
                {
                    Checked = !Checked;
                    if (OnRadioButtonClicked != null)
                    {
                        OnRadioButtonClicked(this, Checked);
                    }
                    this.DataGridView.InvalidateCell(this);
                }
            }
            base.OnMouseClick(e);
        }

        protected override void OnKeyUp(KeyEventArgs e, int rowIndex)
        {
            base.OnKeyUp(e, rowIndex);
            if (!(IsReadOnly == DataGridViewTriState.True)
                && Display
                && Enabled)
            {
                if (e.KeyCode == Keys.Space)
                {
                    Checked = !Checked;
                    if (OnRadioButtonClicked != null)
                    {
                        OnRadioButtonClicked(this, Checked);
                        this.DataGridView.InvalidateCell(this);
                    }
                }
            }
        }
        #endregion
    }
    /// <summary>
    /// 状态变化事件
    /// </summary>
    public delegate void RadioButtonClickedHandler(object sender, bool state);
    public class DataGridViewRadioButtonCellEventArgs : EventArgs
    {
        bool _bChecked;
        public DataGridViewRadioButtonCellEventArgs(bool bChecked)
        {
            _bChecked = bChecked;
        }

        public bool Checked
        {
            get { return _bChecked; }
        }
    }

列:

View Code
/// <summary>
    /// DataGridView单选列
    /// </summary>
    public class DataGridViewRadioButtonColumn : DataGridViewColumn
    {
        #region Property

        private bool _isReadOnly = false;
        /// <summary>
        /// 是否只读
        /// </summary>
        [Category("行为")]
        [Description("是否只读")]
        [DefaultValue(false)]
        [Browsable(true)]
        [RefreshProperties(RefreshProperties.Repaint)]
        public bool IsReadOnly
        {
            get { return _isReadOnly; }
            set
            {
                _isReadOnly = value;
            }
        }

        /// <summary>
        /// 是否只读
        /// </summary>
        [Category("行为")]
        [Description("是否只读")]
        [DefaultValue(true)]
        [Browsable(false)]
        public new bool ReadOnly
        {
            get { return true; }
            set
            {
                base.ReadOnly = true;
            }
        }

        private bool _isSingleOn = true;

        /// <summary>
        /// 是否启用单行选中,单行模式,会自动控制其他行(不包括隐藏行)的选择
        /// </summary>
        [Category("行为")]
        [Description("是否启用单行选中,单行模式,会自动控制其他行(不包括隐藏行)的选择")]
        [DefaultValue(true)]
        [Browsable(true)]
        public bool SingleOn
        {
            get { return _isSingleOn; }
            set
            {
                _isSingleOn = value;
            }
        }

        #endregion

        public DataGridViewRadioButtonColumn()
        {
            base.ReadOnly = true;
            base.CellTemplate = new DataGridViewRadioButtonCell();
        }

        /// <summary>
        /// 保证属性编辑器里修改的值能保存
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            DataGridViewRadioButtonColumn col = (DataGridViewRadioButtonColumn)base.Clone();
            col.IsReadOnly = IsReadOnly;
            col.ReadOnly = true;
            col.SingleOn = SingleOn;
            return col;
        }

    }

 参考:http://blog.163.com/szs1980@126/blog/static/478922032012124102158334/

转载于:https://www.cnblogs.com/zcfsky/archive/2013/02/26/2934233.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您想要将现有的 C# 项目迁移到 C# Core 吗?如果是这样,您可以按照以下步骤进行操作: 1. 首先,确保您已经安装了 .NET Core SDK,您可以在 Microsoft 的官方网站上下载并安装它。 2. 打开您的 C# 项目,并确保它在 Visual Studio 可正常编译和运行。 3. 创建一个新的 C# Core 项目。您可以使用 Visual Studio 或者使用命令行工具(如 dotnet CLI)来完成这一步骤。 4. 将原有项目的代码和文件复制到新的 C# Core 项目。请注意,某些特定的功能、类库或者 API 可能需要进行适当的修改或者替换,以适应 C# Core 的要求。 5. 更新项目的依赖项。C# Core 使用 NuGet 包管理器来管理依赖项。您可以使用 Visual Studio 或者 dotnet CLI 来添加、更新或者删除项目的依赖项。 6. 对于任何特定于平台的代码,您可能需要进行一些调整。C# Core 是跨平台的,因此某些特定于平台的 API 或者功能可能需要进行适当的修改。 7. 进行测试并调试。确保您的 C# Core 项目能够正确地编译、运行和提供预期的功能。 8. 最后,根据需要,您可以部署和发布您的 C# Core 项目。您可以将其打包为一个独立的可执行文件,或者作为一个 ASP.NET Core Web 应用程序进行部署。 这些是一般的步骤,具体的迁移过程可能会因项目的复杂性和特定要求而有所不同。请确保在迁移之前备份您的现有项目,并在迁移过程进行适当的测试和验证。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值