How to add two checkboxes in the data grid view column?
Asked
Active
Viewed 89 times
-1
-
have you tried create a usercontrol with two checkboxes? – Sebastian Siemens Jan 07 '19 at 12:15
-
that I will create, but I don't know how that will add in data grid view cell. – Nandu Jan 07 '19 at 12:27
-
What's your requirement? Let's say you have such column type, but what should be the data type of the property to which the column is bound to? – Reza Aghaei Jan 07 '19 at 12:33
1 Answers
1
OK, that's not the simple task. But can be done :) Simple user control is not the solution.
You have to create proper template for a cell and then create a control (may be user control) with proper interface.
First, you have to create new control type. I think that it can be simple usercontrol with some additional stuff:
class CheckBoxesState
{
public bool Ch1Checked {get;set;}
public bool Ch2Checked {get;set;}
}
class CheckBoxesControl: UserControl, IDataGridViewEditingControl
{
DataGridView dataGridView;
private bool valueChanged = false;
int rowIndex;
CheckBoxesState state;
// Implements the IDataGridViewEditingControl.EditingControlFormattedValue
// property.
public object EditingControlFormattedValue
{
get { return state; }
set
{
if(value is CheckBoxesState)
{
state = value;
//change checkboxes state in you user control
}
}
}
// Implements the
// IDataGridViewEditingControl.GetEditingControlFormattedValue method.
public object GetEditingControlFormattedValue(
DataGridViewDataErrorContexts context)
{
return EditingControlFormattedValue;
}
// Implements the
// IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.
public void ApplyCellStyleToEditingControl(
DataGridViewCellStyle dataGridViewCellStyle)
{
this.Font = dataGridViewCellStyle.Font;
}
// Implements the IDataGridViewEditingControl.EditingControlRowIndex
// property.
public int EditingControlRowIndex
{
get
{
return rowIndex;
}
set
{
rowIndex = value;
}
}
// Implements the IDataGridViewEditingControl.EditingControlWantsInputKey
// method.
public bool EditingControlWantsInputKey(
Keys key, bool dataGridViewWantsInputKey)
{
return !dataGridViewWantsInputKey;
}
// Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit
// method.
public void PrepareEditingControlForEdit(bool selectAll)
{
// No preparation needs to be done.
}
// Implements the IDataGridViewEditingControl
// .RepositionEditingControlOnValueChange property.
public bool RepositionEditingControlOnValueChange
{
get
{
return false;
}
}
// Implements the IDataGridViewEditingControl
// .EditingControlDataGridView property.
public DataGridView EditingControlDataGridView
{
get
{
return dataGridView;
}
set
{
dataGridView = value;
}
}
// Implements the IDataGridViewEditingControl
// .EditingControlValueChanged property.
public bool EditingControlValueChanged
{
get
{
return valueChanged;
}
set
{
valueChanged = value;
}
}
// Implements the IDataGridViewEditingControl
// .EditingPanelCursor property.
public Cursor EditingPanelCursor
{
get
{
return base.Cursor;
}
}
protected override void OnValueChanged(EventArgs eventargs)
{
// Notify the DataGridView that the contents of the cell
// have changed.
valueChanged = true;
this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
base.OnValueChanged(eventargs);
}
}
Next, you have to create new cell type.
public class CheckBoxesCell : DataGridViewCell
{
public CheckBoxesCell()
: base()
{
}
public override Type EditType
{
get
{
// Return the type of the editing control that cell uses.
return typeof(CheckBoxesControl);
}
}
public override Type ValueType
{
get
{
// Return the type of the value that Cell contains.
return typeof(CheckBoxesState);
}
}
public override object DefaultNewRowValue
{
get
{
// Use the current date and time as the default value.
return new CheckBoxesState();
}
}
}
The next thing you should create is new column type for data grid:
public class CheckBoxesColumn : DataGridViewColumn
{
public CheckBoxesColumn() : base(new CheckBoxesCell())
{
}
public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
// Ensure that the cell used for the template is a CheckBoxesCell.
if (value != null &&
!value.GetType().IsAssignableFrom(typeof(CheckBoxesCell)))
{
throw new InvalidCastException("Must be a CheckBoxesCell");
}
base.CellTemplate = value;
}
}
}
And that should be all. Now you only have to create CheckBoxesColumn and add it to your datagrid.
Everything is shown here: https://learn.microsoft.com/en-us/dotnet/framework/winforms/controls/how-to-host-controls-in-windows-forms-datagridview-cells

Adam Jachocki
- 1,897
- 1
- 12
- 28