Hi,
I have read the article "How to: Host Controls in Windows Forms DataGridView Cells "
, and I don't find how to host my own user control in a DataGridView.
I took the above example and changed the base class from DateTimePicker to CheckBoxUserControl in the class CalendarEditingControl.(I also changed all DateTime type to string).
CheckBoxUserControl contains a few common standard checkbox controls that can be represented as one string value.
What I want is that in edit mode, my control will show up (like the calendar).
but it doesn't, can someone give a hand here
here is my modified module:
using System;
using System.Windows.Forms;
namespace CheckBox_Custom_Cell
{
public class CheckBoxColumn : DataGridViewColumn
{
public CheckBoxColumn()
: base(new CheckBoxCell())
{
}
public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
if (value != null &&
!value.GetType().IsAssignableFrom(typeof(CheckBoxCell)))
{
throw new InvalidCastException("Must be a CheckBoxCell");
}
base.CellTemplate = value;
}
}
}
public class CheckBoxCell : DataGridViewTextBoxCell
{
public CheckBoxCell()
: base()
{
}
public override void InitializeEditingControl(int rowIndex, object
initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
base.InitializeEditingControl(rowIndex, initialFormattedValue,
dataGridViewCellStyle);
CheckBoxEditingControl ctl =
DataGridView.EditingControl as CheckBoxEditingControl;
ctl.Value = this.Value.ToString();
}
public override Type EditType
{
get
{
return typeof(CheckBoxEditingControl);
}
}
public override Type ValueType
{
get
{
return typeof(string);
}
}
public override object DefaultNewRowValue
{
get
{
return "Init";
}
}
}
class CheckBoxEditingControl : CheckBoxUserControl, IDataGridViewEditingControl
{
DataGridView dataGridView;
private bool valueChanged = false;
int rowIndex;
public CheckBoxEditingControl()
{
}
public object EditingControlFormattedValue
{
get
{
return this.Value;
}
set
{
String newValue = value as String;
if (newValue != null)
{
this.Value = newValue;
}
}
}
public object GetEditingControlFormattedValue(
DataGridViewDataErrorContexts context)
{
return EditingControlFormattedValue;
}
public void ApplyCellStyleToEditingControl(
DataGridViewCellStyle dataGridViewCellStyle)
{
this.Font = dataGridViewCellStyle.Font;
this.ForeColor = dataGridViewCellStyle.ForeColor;
this.BackColor = dataGridViewCellStyle.BackColor;
}
public int EditingControlRowIndex
{
get
{
return rowIndex;
}
set
{
rowIndex = value;
}
}
public bool EditingControlWantsInputKey(
Keys key, bool dataGridViewWantsInputKey)
{
switch (key & Keys.KeyCode)
{
case Keys.Left:
case Keys.Up:
case Keys.Down:
case Keys.Right:
case Keys.Home:
case Keys.End:
case Keys.PageDown:
case Keys.PageUp:
return true;
default:
return false;
}
}
public void PrepareEditingControlForEdit(bool selectAll)
{
// No preparation needs to be done.
}
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 checkedListBox1_SelectedValueChanged(object sender, EventArgs e)
{
// Notify the DataGridView that the contents of the cell
// have changed.
valueChanged = true;
this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
base.checkedListBox1_SelectedValueChanged(sender, e);
}
}
}

Hosting custom user control in a DataGridView
SeanLambert
Now that I’m older and smarter
< xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" /> < xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /> let me rephrase my question.
What I would like to have is the following behavior:
1. A cell containing text and some kind of a drop down button (like combo box)
2. Clicking on the drop down button will display my user control
3. The control will be closed by the esc key
4. A string value will be returned from the control and will be the value of the cell
I need help or example regarding steps 1 & 2
10x