Property Grid - TypeConverter

R

RedLars

Hi,

Trying to get an handle on the property grid which I'm finding very
handy.

I got one problem though that I have not been able to fix.

Here is a small example to illustrate the problem;

Say I want to display a list of employee's with their Name, Position
and Salary in a propertygrid. So far so good.
public class Employee
{
private string _Name = "";
private string _Position = "";
private int _Salary = "";

[DisplayNameAttribute("Employee Name")]
public string Name
{
get { return _Name}
set { _Name= value; }
}

[DisplayNameAttribute("Employee Position),
DescriptionAttribute("TODO")]
public string Position
{
get { return _Position}
set { _Position= value; }
}

[TypeConverter(typeof(ListConverter)),
DisplayNameAttribute("Employee Salary"), DescriptionAttribute("TODO")]
public int Salary
{
get { return _Salary}
set { _Salary= value; }
}
}

By adding a ListConverter I'm able to get a drop down list of all the
options available. However, the same list would apply for all
employees. How can I associate each employee with its own
ListConverter object (with different salaries) ? Say for instance I
wanted to have different salary ranges for each position (Director,
Manager etc).

public class ListConverter : StringConverter
{
public override bool
GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
public override StandardValuesCollection
GetStandardValues(ITypeDescriptorContext context)
{
return new StandardValuesCollection(new string[]
{ "£20,000", "£25,000", "£30,000", "£35,000", "£40,000" });
}
public override bool
GetStandardValuesExclusive(ITypeDescriptorContext context)
{
return false;
}
}


Appreciate any help.
 
S

Stoitcho Goutsev \(100\)

ReadLars,
When a method of the type converter is called it is passed with one paramter
*context*. The context has a property Instance that is a reference to the
object that is currently edited. In the GetStandardValues method you can use
the context to check properties of the object and provide different list of
salaries depending on the values of some other properties e.g. Position.

Make sure you check the *context* parameter and the *Instance* proeprty for
*null* before using it.


--
HTH
Stoitcho Goutsev (100)

Hi,

Trying to get an handle on the property grid which I'm finding very
handy.

I got one problem though that I have not been able to fix.

Here is a small example to illustrate the problem;

Say I want to display a list of employee's with their Name, Position
and Salary in a propertygrid. So far so good.
public class Employee
{
private string _Name = "";
private string _Position = "";
private int _Salary = "";

[DisplayNameAttribute("Employee Name")]
public string Name
{
get { return _Name}
set { _Name= value; }
}

[DisplayNameAttribute("Employee Position),
DescriptionAttribute("TODO")]
public string Position
{
get { return _Position}
set { _Position= value; }
}

[TypeConverter(typeof(ListConverter)),
DisplayNameAttribute("Employee Salary"), DescriptionAttribute("TODO")]
public int Salary
{
get { return _Salary}
set { _Salary= value; }
}
}

By adding a ListConverter I'm able to get a drop down list of all the
options available. However, the same list would apply for all
employees. How can I associate each employee with its own
ListConverter object (with different salaries) ? Say for instance I
wanted to have different salary ranges for each position (Director,
Manager etc).

public class ListConverter : StringConverter
{
public override bool
GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
public override StandardValuesCollection
GetStandardValues(ITypeDescriptorContext context)
{
return new StandardValuesCollection(new string[]
{ "£20,000", "£25,000", "£30,000", "£35,000", "£40,000" });
}
public override bool
GetStandardValuesExclusive(ITypeDescriptorContext context)
{
return false;
}
}


Appreciate any help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top