Conversion to string of what? Of the whole DataInt object? But I don't need that. I need to make editable a single property of DataInt.
Well, but why you have no overriden DataInt.ToString() method? Problem in that when column trying to convert cell's data to string it tries to find TypeConverter. In case with your type column receives base converter, that converts data to it's type.
It will more correct if you will attach to abstract class universal converter:
[c#]
[TypeConverter(typeof(Data.DataConverter))]
public abstract class Data
{
public class DataConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string) ? true : base.CanConvertFrom (context, sourceType);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
return destinationType == typeof(string) ? true : base.CanConvertTo (context, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if (context != null && value is string)
{
//get current value
Data data = context.Instance as Data;
//parse string
if (data != null)
data.Parse(value as string);
//return modified data
return data;
}
return base.ConvertFrom (context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (context != null && destinationType == typeof(string))
{
//get current value
Data data = context.Instance as Data;
return data == null || data.Value == null ? string.Empty : data.Value.ToString();
}
return base.ConvertTo (context, culture, value, destinationType);
}
}
private string _ID;
public string DisplayName
{
get{ return _ID; }
set{ _ID = value; }
}
protected object _Value;
public virtual object Value
{
get{ return _Value; }
set{ _Value = value; }
}
//new method that helps type converter to convert value from string
public abstract void Parse(string value);
public Data()
{
_ID = \"Name\" + GetNextId();
_Value = null;
}
public static int GetNextId()
{
return CurId++;
}
private static int CurId;
static Data()
{
CurId = 1;
}
}
public class DataInt : Data
{
public DataInt()
{
_Value = GetNextId();
}
public override void Parse(string value)
{
this.Value = int.Parse(value);
}
}
public class DataString : Data
{
public DataString()
{
_Value = \"s\"+GetNextId().ToString();
}
public override void Parse(string value)
{
//may be here you need some validation
this.Value = value;
}
}