I am adding the Property grid control in my project. I have to show the Drop down box in one field of Property Grid. Is there any solution to apply this.
Asked
Active
Viewed 1.1k times
15
-
Use `Template Field` and `ItemTemplate` in your `GridView`. – Ullas Jul 01 '14 at 06:10
-
1No, That's not grid. That is PropertyGrid control in windows form. – Manivijay Jul 01 '14 at 06:20
-
This link might help you more than the accepted answer -> https://www.codeproject.com/Articles/23242/Property-Grid-Dynamic-List-ComboBox-Validation-and Although I think it might be a slight overkill to use a ListBox, I think just a good olde in-memory List would do the job – Luke T O'Brien Jul 21 '17 at 13:23
1 Answers
20
You have to declare a type editor for the property in your PropertyGrid
and then add to the list of choices. This example creates a Type Converter and then overrides the GetStandardValues()
method to provide choices to the drop-down:
private String _formatString = null;
[Category("Display")]
[DisplayName("Format String")]
[Description("Format string governing display of data values.")]
[DefaultValue("")]
[TypeConverter(typeof(FormatStringConverter))]
public String FormatString { get { return _formatString; } set { _formatString = value; } }
public class FormatStringConverter : StringConverter
{
public override Boolean GetStandardValuesSupported(ITypeDescriptorContext context) { return true; }
public override Boolean GetStandardValuesExclusive(ITypeDescriptorContext context) { return true; }
public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
List<String> list = new List<String>();
list.Add("");
list.Add("Currency");
list.Add("Scientific Notation");
list.Add("General Number");
list.Add("Number");
list.Add("Percent");
list.Add("Time");
list.Add("Date");
return new StandardValuesCollection(list);
}
}
The key is the property being assigned a Type Converter in the line:
[TypeConverter(typeof(FormatStringConverter))]
That provides you with the opportunity to introduce your own behavior via overrides.
Here's a simpler example, which allows the Enum type of a Property to automatically provide its values to the PropertyGrid
drop-down:
public enum SummaryOptions
{
Sum = 1,
Avg,
Max,
Min,
Count,
Formula,
GMean,
StdDev
}
private SummaryOptions _sumType = SummaryOptions.Sum;
[Category("Summary Values Type")]
[DisplayName("Summary Type")]
[Description("The summary option to be used in calculating each value.")]
[DefaultValue(SummaryOptions.Sum)]
public SummaryOptions SumType { get { return _sumType; } set { _sumType = value; } }
By virtue of the fact that the property is an Enum type, those enum values pass through to become the drop-down options automatically.

DonBoitnott
- 10,787
- 6
- 49
- 68
-
using this, how can I show dynamic elements in the dropdown? how can i pass a custom list to `FormatStringConverter` for different properties? – mrid Dec 27 '19 at 11:50
-
You might want to post that as a new question. That's probably more involved that you realize. If you truly mean "dynamic", the biggest issue might be refreshing it as you want changes. This is more of a "grab it once" kind of list, not a list that should be ever-changing. – DonBoitnott Dec 27 '19 at 15:50