我正在尝试创建一个由 Radiobuttons 托管的 DataGridView 列。我一直在关注这篇MSDN 文章。
尽管我已经更改了教程中的代码来编写自己的类,但它并没有按预期工作。问题是我不完全确定我错过了什么。编译时不会出错。但它显示为复选框而不是单选按钮。
我已在此处附加了 Visual Studio 项目。对于那些不确定下载未知附件的人,这里是我写的 3 个类。
RadiobuttonColumn
班级。它继承了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");
}
}
}
}
}
UPDATED RadiobuttonCell
类继承DataGridViewCell
类。
using System;
using System.Windows.Forms;
using System.Drawing;
namespace DataGridView_Radiobutton_column
{
public class RadiobuttonCell : DataGridViewCell
{
public RadiobuttonCell()
: base()
{
}
public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
RadiobuttonEditingControl rdb = DataGridView.EditingControl as RadiobuttonEditingControl;
if (this.Value == null)
{
rdb.Checked = false;
}
else
{
rdb.Checked = (Boolean)this.Value;
}
}
public override Type EditType
{
get
{
return typeof(RadiobuttonEditingControl);
}
}
public override Type ValueType
{
get
{
return typeof(Boolean);
}
}
public override object DefaultNewRowValue
{
get
{
return false;
}
}
protected override void Paint(System.Drawing.Graphics graphics, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
{
base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
Rectangle rectRadioButton = default(Rectangle);
rectRadioButton.Width = 14;
rectRadioButton.Height = 14;
rectRadioButton.X = cellBounds.X + (cellBounds.Width - rectRadioButton.Width) / 2;
rectRadioButton.Y = cellBounds.Y + (cellBounds.Height - rectRadioButton.Height) / 2;
ControlPaint.DrawRadioButton(graphics, rectRadioButton, ButtonState.Normal);
}
}
}
该类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;
}
}
}
如果有人可以看看这个并指出我应该在代码中进行哪些更改的正确方向,我将不胜感激。
谢谢你。