我创建了一个自定义控件来编辑DataGridView单元格中的自定义值。我在这里关注这个例子: How to: Host Controls in Windows Forms DataGridView Cells
我有一个自定义数据类,它是绑定到DataGridView的对象列表的成员。
internal class CustomValue
{
// Some stuff is here.
}
我创建了一个自定义控件来编辑实现IDataGridViewEditingControl接口的单元格中的值。
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;
}
}
}
我已经设置了一个自定义DataGridViewCell,它来自DataGridViewTextBoxCell,因此单元格只显示自定义值的字符串表示形式,直到它被编辑。
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();
}
}
}
我创建了一个自定义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;
}
}
}
单元格正确显示自定义类型的值,我可以使用自定义控件编辑值。当我离开编辑单元格时,我得到一个System.FormatException:在System.Convert.DefaultToType(IConvertable值,Type targetType,IFormatProvider provider)中,从'System.String'到'CustomValue'的无效转换......
在什么时候尝试将字符串转换为自定义值?这应该由CustomValueEditControl.GetEditingControlFormattedValue处理吗?
答案 0 :(得分:1)
在什么时候尝试将字符串转换为自定义值?这应该由CustomValueEditControl.GetEditingControlFormattedValue处理吗?
完全。但是您所基于的样本无法正确处理它。你应该使用这样的东西:
var test = '1';
$(...).filter(function () {
return $(this).text() === test;
});
如果您返回public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
{
if ((context & DataGridViewDataErrorContexts.Parsing) != 0)
{
// Here you should not return string, but your value
return Value;
}
return EditingControlFormattedValue;
}
(string
属性确实存在),则EditingControlFormattedValue
会尝试将其转换为DataGridView
对象。该示例有效,因为CustomValue
已关联TypeConverter
,而您的班级显然没有。{/ p>
答案 1 :(得分:0)
所以我最终通过实现TypeConverter类来解决这个问题。
internal class CustomValueTypeConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string))
{
return true;
}
return base.CanConvertTo(context, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if (value is string)
{
CustomValue customValue;
if (CustomValue.TryParse((string)value, out customValue))
{
return customValue;
}
}
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string))
{
value.ToString();
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
然后我不得不用TypeConverterAttribute修饰CustomValue类。
[TypeConverter(typeof(CustomValueTypeConverter))]
internal class CustomValue
{
// Some stuff here.
}