I have created a custom control to edit a custom value in a DataGridView cell. I am following the example here: How to: Host Controls in Windows Forms DataGridView Cells
I have a custom data class that is a member of a list of objects bound to the DataGridView.
internal class CustomValue
{
// Some stuff is here.
}
I have created a custom control to edit the value in the cell that implements the IDataGridViewEditingControl interface.
internal partial class CustomValueEditControl : UserControl, IDataGridViewEditingControl
{
DataGridView m_dataGridView;
private bool m_valueChanged = false;
int m_rowIndex;
private CustomValue m_value;
public CustomValue Value
{
get
{
return m_value;
}
set
{
m_value = value;
}
}
public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
{
}
public DataGridView EditingControlDataGridView
{
get
{
return m_dataGridView;
}
set
{
m_dataGridView = value;
}
}
public object EditingControlFormattedValue
{
get
{
return this.Value.ToString();
}
set
{
if (value is String)
{
CustomValue val;
if (CustomValue.TryParse((String)value, out val))
{
this.Value = val;
}
else
{
this.Value = new CustomValue();
}
}
}
}
public int EditingControlRowIndex
{
get
{
return m_rowIndex;
}
set
{
m_rowIndex = value;
}
}
public bool EditingControlValueChanged
{
get
{
return m_valueChanged;
}
set
{
m_valueChanged = value;
}
}
public bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey)
{
switch (keyData & Keys.KeyCode)
{
case Keys.Up:
case Keys.Down:
return true;
default:
return !dataGridViewWantsInputKey;
}
}
public Cursor EditingPanelCursor
{
get
{
return base.Cursor;
}
}
public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
{
return EditingControlFormattedValue;
}
public void PrepareEditingControlForEdit(bool selectAll)
{
}
public bool RepositionEditingControlOnValueChange
{
get
{
return false;
}
}
}
I have set up a custom DataGridViewCell that descends from DataGridViewTextBoxCell so the cell just displays a string representation of the custom value until it is edited.
internal class CustomValueCell : DataGridViewTextBoxCell
{
public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
CustomValueEditControl customValueEditControl = DataGridView.EditingControl as CustomValueEditControl;
if (this.Value == null)
{
customValueEditControl.Value = (CustomValue)this.DefaultNewRowValue;
}
else
{
customValueEditControl.Value = (CustomValue)this.Value;
}
}
public override Type EditType
{
get
{
return typeof(CustomValueEditControl);
}
}
public override Type ValueType
{
get
{
return typeof(CustomValue);
}
}
public override Type FormattedValueType
{
get
{
return typeof(string);
}
}
public override object DefaultNewRowValue
{
get
{
return new CustomValue();
}
}
}
I have created a custom DataGridViewColumn.
internal class CustomValueColumn : DataGridViewColumn
{
public CustomValueColumn()
: base(new CustomValueCell())
{
}
public override object Clone()
{
CustomValueColumn clone = (CustomValueColumn)base.Clone();
return clone;
}
public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
// Ensure that the cell used for the template is a CustomValueCell.
if (value != null && !value.GetType().IsAssignableFrom(typeof(CustomValueCell)))
{
throw new InvalidCastException("Must be a CustomValueCell");
}
base.CellTemplate = value;
}
}
}
The cell shows the value of the custom type correctly, and I can use the custom control to edit the value. When I leave the editing cell, I get a System.FormatException: Invalid cast from 'System.String' to 'CustomValue' at System.Convert.DefaultToType(IConvertable value, Type targetType, IFormatProvider provider)...
At what point is it trying to cast a string to my custom value? Should this not be handled by the CustomValueEditControl.GetEditingControlFormattedValue?