我有一个非常简单的类,我想在Datagridview中显示。对于一个属性,我想显示一个带有可能选择的Dropbox。我以为我可以用一个typeconverter完成这个,但要么我错了,要么我不知道....
这是我在DataGridView中显示的类:
public class CommandlineArgument
{
public CommandlineArgumentType ArgumentType { get; set; }
public string ArgumentName { get; set; }
[TypeConverter(typeof(ParameterConverter))]
public string ArgumentValue { get; set; }
[TypeConverter(typeof(ConditionCheckConverter))]
public int ConditionCheck { get; set; }
public CommandlineArgument(CommandlineArgumentType argumentType, string argumentName, string argumentValue)
{
ArgumentType = argumentType;
ArgumentName = argumentName;
ArgumentValue = argumentValue;
ConditionCheck = 0;
}
}
那就是TypeConverter:
public class ConditionCheckConverter : TypeConverter
{
public static PDSPackage CurrentlyEditedPackage;
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
{
return true;
}
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
string[] possibleValues = new string[1];
possibleValues = CurrentlyEditedPackage.ConditionChecks.Select(cc => cc.Name).ToArray();
return new StandardValuesCollection(possibleValues);
}
#region Convert To
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(String)) { return true; }
return false;
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
string ccname = CurrentlyEditedPackage.ConditionChecks.Where(cc => cc.ID == Convert.ToInt32(value)).First().Name;
return ccname;
}
#endregion
}
如果我将DatagridViewColumn设置为Combobox,我会收到异常。如果将它设置为一个简单的TextBox,我就不会得到一个Dropdown ...
修改
好的,这是我的ConvertFrom方法:
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
int CheckID = CurrentlyEditedPackage.ConditionChecks.Where(cc => cc.Name == (string) value).First().ID;
return CheckID;
}
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(String)) { return true; }
return false;
}
但我没有帮助。我在每个方法中设置了断点,但似乎唯一被称为ConvertTo