Wednesday, March 19, 2014

enum description in arabic and how to bind enum value to integer , get arabic description to enum in c#

the main purpose for this example
1- how to create Enum in separate Class
2- how to get enum integer value to bind it to function .
3- how to get enum name and enum arabic description and bind it to gridview function call.

first create class called Enum_Class to prepare enum sample as follow
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.Reflection;
/// <summary>
/// Summary description for Enum_Class
/// </summary>
public class Enum_Class
{
    #region Enum For Medical Reports
       public enum VACATION_REPORTS_REQUEST_STATUS_Enum : int
    {
        
        [Description("&quot;التحضير&quot;")]
        Preparation = 1,
        /// <summary>
        ///
        /// </summary>
         [Description("مكتمل")]
        Complete = 2,
         [Description("مقبول")]
        Acceptable = 3,
          [Description("غير مكتمل-مرفوض")]
        Incomplete_OR_Rejected = 4,
       
         [Description("موافقة نهائية")]
        Final_approved = 5,
     
         [Description("أرشفةوإغلاق")]
         Archiving_And_Close = 6
    }

   
    #endregion

    #region Methods
    public static string GetDescription(object enumValue, string defDesc)
    {
        FieldInfo fi = enumValue.GetType().GetField(enumValue.ToString());

        if (null != fi)
        {
            object[] attrs = fi.GetCustomAttributes(typeof(DescriptionAttribute), true);
            if (attrs != null && attrs.Length > 0)
                return ((DescriptionAttribute)attrs[0]).Description;
        }

        return defDesc;
    }
    #endregion
}
--------------------------------------------------
second Step
how to parse Enum value to integer value
int X =  (int)Enum_Class.VACATION_REPORTS_REQUEST_STATUS_Enum.Preparation;
-----------------------------------------------------
third
how to get Enum string using it's value , and get Description arabic text
and bind value to gridview
 <asp:TemplateField HeaderText="الحالة ">
                                    <ItemTemplate>
                                        <asp:Label ID="LBL_Status" runat="server" Text='<%#VACATION_REPORTS_REQUEST_GET_STATUS(Eval("REQUEST_STATUS_ID")) %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>


  public string VACATION_REPORTS_REQUEST_GET_STATUS(object STATUS_ID)
    {
        string result = string.Empty;
        Enum_Class.VACATION_REPORTS_REQUEST_STATUS_Enum enumStatus = ((Enum_Class.VACATION_REPORTS_REQUEST_STATUS_Enum)int.Parse(STATUS_ID.ToString()));
        result = Enum_Class.GetDescription(enumStatus, result);
         return result;
    }

 
 

No comments:

Post a Comment