http://www.aygfsteel.com/ebecket 返還網
          隨筆-140  評論-11  文章-131  trackbacks-0

          I have an enum SortFilter, which looks like following:

          publicenum SortFilter

          {

                FirstName,

                LastName,

                Age,

                Experience

          }

          Now, let's say I want to display the string value of enum in some control. For that, I will have to convert Enum value to string. For example, I want to add all enum string values to a DropDownList. The following code loops through the enumeration and adds string values to it. Here SortByList is DropDownList.

          SortByList.Items.Clear();

          // Conversion from Enum to String

          foreach (string item in Enum.GetNames(typeof(ArrayListBinding.SortFilter)))

          {

                SortByList.Items.Add(item);
          }    

          This code converts an enum to string:

          string name= Enum.GetName(typeof(ArrayListBinding.SortFilter), SortFilter.FirstName);

          Now let's say, you have an enum string value say, "FirstName" and now you want to convert it to Enum value. The following code converts from a string to enum value, where Developer.SortingBy is of type SortFilter enumeration:

          // Conversion from String to Enum

          Developer.SortingBy = (SortFilter)Enum.Parse(typeof(SortFilter), "FirstName");  

          posted on 2009-11-20 17:53 becket_zheng 閱讀(379) 評論(0)  編輯  收藏 所屬分類: C#
          主站蜘蛛池模板: 海伦市| 霍山县| 乾安县| 贵德县| 大余县| 江孜县| 逊克县| 竹山县| 崇左市| 华阴市| 江阴市| 修武县| 广安市| 房产| 绥中县| 明光市| 安溪县| 潮州市| 焦作市| 定南县| 蒙阴县| 宜宾县| 大英县| 华蓥市| 广宗县| 基隆市| 忻城县| 双鸭山市| 苍南县| 本溪| 洛宁县| 阿拉善右旗| 涿州市| 台江县| 当雄县| 广昌县| 四川省| 富民县| 克拉玛依市| 武宣县| 渭南市|