简体   繁体   中英

How to compare combo enum with actual enum

Guys I loded a combobox from Enum

  var values = from P_ProgramType.ReportType enumValue in Enum.GetValues(typeof(P_ProgramType.ReportType))
                     select new  KeyValuePair<P_ProgramType.ReportType, string>((enumValue), enumValue.ToString());
        reportTypeComboBox.DataSource = values.OrderByDescending(x => x.Value).ToList(); ;
       reportTypeComboBox.DisplayMember = "Value";
       reportTypeComboBox.ValueMember = "Key";  

Now I need top check what value is selecting in each selected index to do the further work . Like

if ((P_ProgramType.ReportType)reportTypeComboBox.SelectedItem == P_ProgramType.ReportType.OVERALL)
        {
            lvlReportTypeNote.Text = "OVERALL : ALL (Including Secondary Loan) ";
        }

But above comparison is not working .... Need a way to do that ??

Assuming your ReportType is of type int then:

if (Convert.ToInt32(reportTypeComboBox.SelectedItemValue) == (int)P_ProgramType.ReportType.OVERALL)

Also consider making yourself an extension method for enums so you can easily populate a drop down:

/// <summary>
///     Represents the various forms of address
/// </summary>
public enum CarMake
{
    [Description("Unknown")]Unknown = -1,
    [Description("Ford Motor Company")] Ford = 1,
    [Description("Nissan Japan")] Nissan = 2,
}

    /// <summary>
    ///     Returns the value attribute content for an enum member.
    /// </summary>
    /// <param name="aEnumMember">Enumeration member to retrieve the description of.</param>
    /// <returns>Returns the contents of the attribute or null if no attribute found</returns>
    public static object GetValue(this Enum aEnumMember)
    {
        Value userValue = GetAttribute<Value>(aEnumMember);
        return userValue != null ? userValue.Val : null;
    }

    /// <summary>
    ///     Returns the description for an enumeration memnber.
    /// </summary>
    /// <param name="aEnumMember">Enumeration member to return the description of via the Description attribute.</param>
    /// <returns>
    ///     Returns the value of the description attribute if present otherwise the string representation of the enum
    ///     member.
    /// </returns>
    /// <example>string text = CarMake.Ford.GetDescription();</example>
    public static string GetDescription(this Enum aEnumMember)
    {
        Type type = aEnumMember.GetType();
        MemberInfo[] memberInfo = type.GetMember(aEnumMember.ToString());

        if (memberInfo.Length > 0)
        {
            object[] attributes = memberInfo[0].GetCustomAttributes(typeof (DescriptionAttribute), false);

            if (attributes.Length > 0)
            {
                return ((DescriptionAttribute) attributes.First()).Description;
            }
        }
        return aEnumMember.ToString();
    }

        /// <summary>
        ///     Constructs a select list where the item desription uses the Description attribute if specified for the member
        ///     or else the name of the member, the value is either the ordinal position in the enum or if specified the Value
        ///     attribute.
        /// </summary>
        /// <param name="aEnumeration">Enumeration to use.</param>
        /// <param name="aSelectedValue">Selected value should the item be selected in the list.</param>
        /// <param name="aIncludeDefaultItem">When true will include aDefaultItemText at the top of the list with a value of null.</param>
        /// <param name="aDefaultItemText"></param>
        /// <returns></returns>
        /// <example>var carSelectList = new CarMake().ToSelectList(CarMake.Ford, true)</example>
        public static SelectList ToSelectList(this Enum aEnumeration, object aSelectedValue = null, bool aIncludeDefaultItem = true, string aDefaultItemText = "(Select)")
        {
            List<SelectListQueryItem<object>> list = (from Enum e in Enum.GetValues(aEnumeration.GetType())
                                                      select new SelectListQueryItem<object>
                                                      {
                                                          Id = e.GetValue() == null ? Enum.Parse(aEnumeration.GetType(), Enum.GetName(aEnumeration.GetType(), e)) : e.GetValue(),
                                                          Name = e.GetDescription()
                                                      }).ToList();

            if (aIncludeDefaultItem)
            {
                list.Insert(0, new SelectListQueryItem<object> {Id = null, Name = aDefaultItemText});
            }
            return new SelectList(list, "ID", "Name", aSelectedValue);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM