Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Radiobutton列创建DataGridView_C#_Datagridview_Datagridviewcolumn - Fatal编程技术网

C# 使用Radiobutton列创建DataGridView

C# 使用Radiobutton列创建DataGridView,c#,datagridview,datagridviewcolumn,C#,Datagridview,Datagridviewcolumn,我正在尝试创建一个带有单选按钮的DataGridView列。我一直在关注MSDN的文章 虽然我已经修改了教程中的代码来编写我自己的类,但它并没有按预期工作。问题是我不完全确定我错过了什么。它在编译时不会出错。但它显示为复选框而不是单选按钮 我已附加Visual Studio项目。对于那些不确定下载未知附件的人,这里是我写的3个类 RadiobuttonColumnclass。它继承了DataGridViewColumn类 using System; using System.Windows.Fo

我正在尝试创建一个带有单选按钮的DataGridView列。我一直在关注MSDN的文章

虽然我已经修改了教程中的代码来编写我自己的类,但它并没有按预期工作。问题是我不完全确定我错过了什么。它在编译时不会出错。但它显示为复选框而不是单选按钮

我已附加Visual Studio项目。对于那些不确定下载未知附件的人,这里是我写的3个类

RadiobuttonColumn
class。它继承了
DataGridViewColumn

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    class RadiobuttonColumn : DataGridViewColumn
    {
        public RadiobuttonColumn()
            : base(new RadiobuttonCell())
        {
        }

        public override DataGridViewCell CellTemplate
        {
            get
            {
                return base.CellTemplate;
            }
            set
            {
                if (value != null && !value.GetType().IsAssignableFrom(typeof(RadiobuttonCell)))
                {
                    throw new InvalidCastException("Must be a RadiobuttonCell");
                }
            }
        }
    }
}
已更新
RadiobuttonCell
类继承了
DataGridViewCell

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    class RadiobuttonColumn : DataGridViewColumn
    {
        public RadiobuttonColumn()
            : base(new RadiobuttonCell())
        {
        }

        public override DataGridViewCell CellTemplate
        {
            get
            {
                return base.CellTemplate;
            }
            set
            {
                if (value != null && !value.GetType().IsAssignableFrom(typeof(RadiobuttonCell)))
                {
                    throw new InvalidCastException("Must be a RadiobuttonCell");
                }
            }
        }
    }
}

RadiobuttonEditingControl
类继承了
RadioButton
类并实现了
IDataGridViewEditingControl
接口的方法

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    class RadiobuttonEditingControl : RadioButton, IDataGridViewEditingControl
    {
        DataGridView dataGridView;
        private bool valueChanged = false;
        int rowIndex;

        public RadiobuttonEditingControl()
        {
            this.Checked = false;
        }

        public object EditingControlFormattedValue
        {
            get
            {
                return this.Checked = true;
            }
            set
            {
                this.Checked = false;
            }
        }

        public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
        {
            return EditingControlFormattedValue;
        }

        public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
        {
        }

        public int EditingControlRowIndex
        {
            get
            {
                return rowIndex;
            }
            set
            {
                rowIndex = value;
            }
        }

        public bool EditingControlWantsInputKey(Keys key, bool dataGridViewWantsInputKey)
        {
            switch (key & Keys.KeyCode)
            {
                case Keys.Space:
                    return true;
                default:
                    return !dataGridViewWantsInputKey;
            }
        }

        public void PrepareEditingControlForEdit(bool selectAll)
        {
        }

        public bool RepositionEditingControlOnValueChange
        {
            get
            {
                return false;
            }
        }

        public DataGridView EditingControlDataGridView
        {
            get
            {
                return dataGridView;
            }
            set
            {
                dataGridView = value;
            }
        }

        public bool EditingControlValueChanged
        {
            get
            {
                return valueChanged;
            }
            set
            {
                valueChanged = value;
            }
        }

        public Cursor EditingPanelCursor
        {
            get
            {
                return base.Cursor;
            }
        }

        protected override void OnCheckedChanged(EventArgs eventArgs)
        {
            valueChanged = true;
            this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
            base.Checked = false;
        }
    }
}
如果有人能看看这一点,并为我指出应该在代码中进行哪些更改的正确方向,我将不胜感激


谢谢。

开始编辑单元格时会发生什么?它变成了一个单选按钮

这是因为您派生自一个复选框单元格,该单元格将自身绘制为复选框

DGV不是控件栅格。它是一个网格框,在用户试图编辑单元格之前,这些框被绘制成控件的样子。此时,DGV将单元格的(一个也是唯一一个)编辑控件移动到位,并使其可见,供用户进行交互

您必须从基本DataGridViewCell类派生,并实现所有的绘制代码

示例代码不必这样做,因为textbox单元格知道如何绘制文本

已更新


本课程可以帮助您。

谢谢您的回答。我已经按照您的指示更改了
RadiobuttonCell
类。请在我的原始帖子中查看更新的代码。我继承了
DataGridViewCell
类,并重写了
Paint
方法。但是我面临一个小问题。DataGridView的背景显示为透明。这是一个截图。我怎样才能解决这个问题?@nK0de您还必须绘制背景。使用单元格的样式确定是否选中了该单元格。我想你可以只使用
graphics.Clear(cellStyle.BackColor:cellStyle.SelectedColor)
。您还需要根据单元格的当前值绘制单选按钮。@nK0de另外,绘制是分阶段进行的,因此您需要使用paintParts参数来确定应该绘制的零件。@nK0de我查看了
DataGridViewCheckBoxCell
的源代码(来自ILSpy)。你应该自己看看。首先要注意的是,它们不使用就地编辑控件(EditType返回null),它们同时处理绘制和交互。绘制相当复杂,因为它们正确处理视觉样式,但代码向您展示了如何绘制每个部分(边框、背景、焦点、前景和错误图标)@nK0de这里的GDI实际上没有太多的功能,因为它们对大多数位使用基类代码,而VisualRenderer或ControlPaint则取决于绘画的其余部分是否启用了视觉样式。ControlPaint很简单,VisualRenderer有点像怪物。我认为您不应该复制和粘贴,但请仔细阅读该代码并使用MSDN了解您可能不理解的特定位,或者在此处提问。