Custom TypeConverter with variable StandardValues
Asked Answered
C

1

7

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

Christenachristendom answered 6/1, 2011 at 14:45 Comment(0)
E
7

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?

Edbert answered 6/1, 2011 at 15:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.