Custom TypeConverter with StandardValues ​​Variable

I have a datagridview with competitor information. I display all the properties of the committer in the PropertyGrid. I want some of these properties (e.g. Degree, City, Institute) to be empty with values ​​taken from the database. For this I can create a custom TypeConvertor similar to this

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 this valueList from the database, and I have 14 such properties, so some kind of universal converter will be much better than 14 converters with the only difference: valueList. Is it possible to create a TypeConverter with the variable valueList (for example, passed to TypeConverter as a parameter in the constructor)? Or maybe there is another way to have a variable value dropbox in the PropertyGrid? Hope this was clear enough Thnx in advance

+6
c # typeconverter propertygrid
source share
1 answer

The GetStandardValues ​​method gives you context. Use context.Instance to access the object that contains your property. Then interrogate him to get a service provider who will provide you database services. It can be through your own API or you can get IServiceProvider and implement GetService, or why not get it through your IOC container as a singleton

+5
source share

All Articles