Wednesday, December 5, 2007

Displaying array as DropDown List in Property Grid

Any list of values can be displayed as a drop down list in the property grid.

By creating a custom converter extended from StringConverter(TypeConverter can also be used), we can display the list of values or value collection in a property grid as a dropdown list..

for example, if you are having a User list, which will updated dynamically, create a converter class as below


public sealed class StringListConverter : StringConverter
{

public static string[] value;
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}

public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
{
return true;
}

public override TypeConverter.StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
{
return new StandardValuesCollection(value);
}
}

the value for the static variable can be set whenever the user list gets changed..

Now the StringListConverter can be used as

[Browsable(true), Description("List of users available")]
[TypeConverter(typeof(StringListConverter))]
public string UserList
{
get { return userList; }
set { userList = value; }
}

[Browsable(false)]
public string[] AvailableUserList
{
set { StringListConverter.value = value; }
}


the purpose of maintaining 2 list is to dynamically update the user list.

2 comments:

radha said...

simple and clear.Keep sharing Artificial Intelligence Online Course

veeraraj said...

Really good information to show through this blog. I really appreciate you for all the valuable information that you are providing us through your blog.
visit : Digital Marketing Training in Chennai || Digital Marketing Course in Chennai