I have a datagridview with info about competitors. I display each copmetitor's properties in PropertyGrid. I want some of those properties (e.g. Degree, City, Institute) to be dropboxes with values taken from database. For this purpose i can create a custom TypeConvertor like this one
class DegreeTypeConverter : StringConverter
{
static string[] _valueList = { "Bachelor", "Master", "Student" };
public override bool GetStandardValuesSupported(
ITypeDescriptorContext context)
{
return true;
}
public override bool GetStandardValuesExclusive(
ITypeDescriptorContext context)
{
return true;
}
public override StandardValuesCollection GetStandardValues(
ITypeDescriptorContext context)
{
return new StandardValuesCollection(_valueList);
}
}
[TypeConverter(typeof(DegreeTypeConverter))]
public string Degree
{
get { return _degree; }
set { _degree = value; }
}
But i want to get that valueList from database and i have 14 such properties so some universal converter would be much better than 14 converters with the only difference: valueList. Is it possible to create a TypeConverter with variable valueList (for example passed into TypeConverter as parameter in constructor)? Or maybe there's another way to have in PropertyGrid a dropbox with variable value list? Hope it was clear enough Thnx in advance
In the GetStandardValues method, you are given a context. Use context.Instance to access the object that holds your property. Then interrogate it to get a service provider that will give you DB services. It could be through your own API or you could derive from IServiceProvider and implement GetService, or why not get it through your IOC container as a singleton?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With