Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom TypeConverter with variable StandardValues

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

like image 610
Sasha Grinevich Avatar asked Jan 06 '11 14:01

Sasha Grinevich


1 Answers

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?

like image 125
Nicolas Cadilhac Avatar answered Sep 18 '22 15:09

Nicolas Cadilhac