Bind Combobox with Enum Description in C#

Bind Combobox with Enum Description in C#

You can bind a ComboBox with Enum description in C# using the following steps:

  • Define an Enum with values and descriptions using the Description attribute:
public enum Gender
{
    [Description("Male")]
    Male = 0,
    [Description("Female")]
    Female = 1,
    [Description("Other")]
    Other = 2
}
  • Create a method that returns the list of the Enum values with their description:
public static List<KeyValuePair<int, string>> GetEnumValues<T>() where T : struct, IConvertible
{
    var values = Enum.GetValues(typeof(T)).Cast<T>();
    return values.Select(e => new KeyValuePair<int, string>(Convert.ToInt32(e), e.GetDescription())).ToList();
}
  • Bind the ComboBox with the list of Enum values using the DisplayMember and ValueMember properties:
comboBox1.DataSource = GetEnumValues<Gender>();
comboBox1.DisplayMember = "Value";
comboBox1.ValueMember = "Key";

In this example, we define an Enum named Gender with values and descriptions using the Description attribute. We then create a method that returns the list of Enum values with their description using the GetDescription() extension method that retrieves the description of the Enum value from the Description attribute.

Finally, we bind the ComboBox with the list of Enum values using the DataSource, DisplayMember and ValueMember properties. The DataSource property is set to the list of Enum values, and the DisplayMember and ValueMember properties are set to "Value" and "Key" respectively to display the Enum descriptions and their corresponding values in the ComboBox.

Note that the GetDescription() method is an extension method defined as follows:

public static string GetDescription(this Enum value)
{
    var field = value.GetType().GetField(value.ToString());

    var attribute = field
        .GetCustomAttributes(typeof(DescriptionAttribute), false)
        .FirstOrDefault() as DescriptionAttribute;

    return attribute != null ? attribute.Description : value.ToString();
}

This extension method is used to retrieve the Description attribute of the Enum value if it exists, or return the string representation of the Enum value if the Description attribute is not present.

Examples

  1. "C# bind combobox to enum with description"

    • Code Implementation:
      var enumValues = Enum.GetValues(typeof(YourEnumType)).Cast<YourEnumType>();
      comboBox.ItemsSource = enumValues.Select(e => new { Value = e, Description = e.GetDescription() });
      comboBox.DisplayMemberPath = "Description";
      comboBox.SelectedValuePath = "Value";
      
  2. "WPF combobox bind to enum with description"

    • Code Implementation:
      var enumValues = Enum.GetValues(typeof(YourEnumType)).Cast<YourEnumType>();
      comboBox.ItemsSource = enumValues.Select(e => new { Value = e, Description = e.GetDescription() });
      comboBox.DisplayMemberPath = "Description";
      comboBox.SelectedValuePath = "Value";
      
  3. "Enum to combobox in C# with description attribute"

    • Code Implementation:
      var enumValues = Enum.GetValues(typeof(YourEnumType)).Cast<YourEnumType>();
      comboBox.ItemsSource = enumValues.Select(e => new { Value = e, Description = e.GetDescription() });
      comboBox.DisplayMemberPath = "Description";
      comboBox.SelectedValuePath = "Value";
      
  4. "Bind enum values to combobox in WinForms C#"

    • Code Implementation:
      var enumValues = Enum.GetValues(typeof(YourEnumType)).Cast<YourEnumType>();
      comboBox.DataSource = enumValues.Select(e => new { Value = e, Description = e.GetDescription() }).ToList();
      comboBox.DisplayMember = "Description";
      comboBox.ValueMember = "Value";
      
  5. "C# WPF combobox enum description attribute"

    • Code Implementation:
      var enumValues = Enum.GetValues(typeof(YourEnumType)).Cast<YourEnumType>();
      comboBox.ItemsSource = enumValues.Select(e => new { Value = e, Description = e.GetDescription() });
      comboBox.DisplayMemberPath = "Description";
      comboBox.SelectedValuePath = "Value";
      
  6. "Combobox enum binding with description in C#"

    • Code Implementation:
      var enumValues = Enum.GetValues(typeof(YourEnumType)).Cast<YourEnumType>();
      comboBox.DataSource = enumValues.Select(e => new { Value = e, Description = e.GetDescription() }).ToList();
      comboBox.DisplayMember = "Description";
      comboBox.ValueMember = "Value";
      
  7. "C# combobox enum description attribute example"

    • Code Implementation:
      var enumValues = Enum.GetValues(typeof(YourEnumType)).Cast<YourEnumType>();
      comboBox.ItemsSource = enumValues.Select(e => new { Value = e, Description = e.GetDescription() });
      comboBox.DisplayMemberPath = "Description";
      comboBox.SelectedValuePath = "Value";
      
  8. "Bind enum to combobox with description in Windows Forms"

    • Code Implementation:
      var enumValues = Enum.GetValues(typeof(YourEnumType)).Cast<YourEnumType>();
      comboBox.DataSource = enumValues.Select(e => new { Value = e, Description = e.GetDescription() }).ToList();
      comboBox.DisplayMember = "Description";
      comboBox.ValueMember = "Value";
      
  9. "C# combobox enum values with description"

    • Code Implementation:
      var enumValues = Enum.GetValues(typeof(YourEnumType)).Cast<YourEnumType>();
      comboBox.ItemsSource = enumValues.Select(e => new { Value = e, Description = e.GetDescription() });
      comboBox.DisplayMemberPath = "Description";
      comboBox.SelectedValuePath = "Value";
      
  10. "Binding Enum to Combobox with Description in C# WPF"

    • Code Implementation:
      var enumValues = Enum.GetValues(typeof(YourEnumType)).Cast<YourEnumType>();
      comboBox.ItemsSource = enumValues.Select(e => new { Value = e, Description = e.GetDescription() });
      comboBox.DisplayMemberPath = "Description";
      comboBox.SelectedValuePath = "Value";
      

More Tags

mvn-repo git-diff heif excel-2007 geocoding unauthorized android-storage tags rounded-corners inline-styles

More C# Questions

More Mixtures and solutions Calculators

More Genetics Calculators

More Statistics Calculators

More Transportation Calculators