Creating a DataGridView with the Radiobutton Column

I am trying to create a DataGridView column hosted with Radiobuttons. I have been following this MSDN article.

Although I modified the code from the tutorial to write my own classes, it does not work properly. The fact is, I'm not quite sure what is missing. There are no errors during compilation. But instead of Radiobuttons, it appears as checkboxes. enter image description here

I attached the Visual Studio project here . And for those who are not sure about downloading unknown attachments, here are 3 classes I wrote.

RadiobuttonColumn . It inherits the DataGridViewColumn class.

 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 The RadiobuttonCell class inherits the DataGridViewCell class.


 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); } } } 

The RadiobuttonEditingControl class inherits the RadioButton class and implements the methods of the IDataGridViewEditingControl interface.

 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; } } } 

If anyone can take a look at this and point me in the right direction regarding what changes should be made to the code, I would be grateful.

Thanks.

+4
source share
1 answer

What happens when you start editing a cell? He turns into a switch.

This is due to what you got from the check box that appears as a check box.

DGV is not a control grid. This is a grid of boxes that are colored to look like controls until the user tries to edit the cell. At this point, the DGV moves the edit control (one and only) to the position and makes it visible to the user.

You will need to get the base class DataGridViewCell and implement all the drawing code.

The sample code should not do this, because a text box cell can draw text.

Update

The ControlPaint class can help you.

+2
source

All Articles