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