This is a migrated thread and some comments may be shown as answers.

Enum description from Description attribute

1 Answer 272 Views
DataEntry
This is a migrated thread and some comments may be shown as answers.
Emin Sadigov
Top achievements
Rank 1
Iron
Veteran
Iron
Emin Sadigov asked on 24 Oct 2019, 01:36 PM

Hello,

Is it any easy way to show enum DisplayValues from enum Description attribute?

public enum ObjectType
    {
        [Description("Textual information in the Html format")]
        TextHtml,
        [Description("Textual information in the Xaml format")]
        TextXaml,
        [Description("Binary Data")]
        Binary
    }

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 28 Oct 2019, 11:08 AM

Hello, Emin,        

You can handle the RadDataEntry.EditorInitialized event and subscribe to the VisualListItemFormatting event of RadDropDownList. Thus, you can customize the Text of each visual item in the drop down: 

        public RadForm1()
        {
            InitializeComponent();

            this.radDataEntry1.EditorInitialized += radDataEntry1_EditorInitialized;
            Item item = new Item(123,"Test", ObjectType.TextXaml);
            this.radDataEntry1.DataSource = item;
        }

        private void radDataEntry1_EditorInitialized(object sender, EditorInitializedEventArgs e)
        {
            RadDropDownList ddl = e.Editor as RadDropDownList;
            if (ddl != null)
            {
                ddl.VisualListItemFormatting -= ddl_VisualListItemFormatting;
                ddl.VisualListItemFormatting += ddl_VisualListItemFormatting;
            }
        }

        private void ddl_VisualListItemFormatting(object sender, VisualItemFormattingEventArgs args)
        {
            if (args.VisualItem.Data.Tag == null)
            {
                args.VisualItem.Data.Tag = GetDescription((ObjectType)args.VisualItem.Data.Value);
            }
            args.VisualItem.Text = args.VisualItem.Data.Tag.ToString();
        }

        public class Item
        {
            public int Id { get; set; }

            public string Name { get; set; }

            public ObjectType Type { get; set; }

            public Item(int id, string name, ObjectType type)
            {
                this.Id = id;
                this.Name = name;
                this.Type = type;
            }
        }

        public enum ObjectType
        {
            [Description("Textual information in the Html format")]
            TextHtml,
            [Description("Textual information in the Xaml format")]
            TextXaml,
            [Description("Binary Data")]
            Binary
        }

        public static string GetDescription (ObjectType objValue) 
        {

            Type type = objValue.GetType();
                Array values = System.Enum.GetValues(type);

                foreach (int val in values)
                {
                    if (val == (int)objValue )
                    {
                        var memInfo = type.GetMember(type.GetEnumName(val));
                        var descriptionAttribute = memInfo[0]
                                                             .GetCustomAttributes(typeof(DescriptionAttribute), false)
                                                             .FirstOrDefault() as DescriptionAttribute;

                        if (descriptionAttribute != null)
                        {
                            return descriptionAttribute.Description;
                        }
                    }
                }
             

            return null; // could also return string.Empty 
        }

An alternative solution is to change the default editor with a RadDropDownList bound to a collection of records using the Description attribute. The following help article demonstrates a sample approach how to plug the editor: https://docs.telerik.com/devtools/winforms/controls/dataentry/how-to/change-the-editor-to-a-bound-raddropdownlist

I have researched in general programming forums to find a suitable solution how to extract the description defined for each enum value: https://www.codeproject.com/Articles/19980/Data-Binding-an-Enum-with-Descriptions You can integrate this approach and bind the RadDropDownList to a collection that would use the description for DisplayMember.

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
DataEntry
Asked by
Emin Sadigov
Top achievements
Rank 1
Iron
Veteran
Iron
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or