1. 程式人生 > >C# DropDownList 綁定枚舉類

C# DropDownList 綁定枚舉類

each eof urn 選擇 play 轉換 type clas pub

第一種

  DropDownList_Franchiser_Type.DataSource = ListTypeForEnum();
  DropDownList_Franchiser_Type.DataValueField = "value";
  DropDownList_Franchiser_Type.DataTextField = "text";
  DropDownList_Franchiser_Type.DataBind() 

  //轉換枚舉類
  public static IList ListTypeForEnum()
        {
            ArrayList list 
= new ArrayList(); foreach (int i in Enum.GetValues(typeof(FranchiserEnum))) { ListItem listitem = new ListItem(Enum.GetName(typeof(FranchiserEnum), i), i.ToString()); list.Add(listitem); } return list; } //第二種 DropDownList_Franchiser_Type.DataSource
= System.Enum.GetNames(typeof(FranchiserEnum)); DropDownList_Franchiser_Type.DataBind(); ListItem li = new ListItem("--請選擇--", ""); this.DropDownList_Franchiser_Type.Items.Insert(0, li); //枚舉 /// <summary> /// 經銷商類別 /// </summary> public
enum FranchiserEnum { [Display(Name = "")] 無 = 0, [Display(Name = "簽約經銷商(B類)")] 簽約經銷商 = 1, [Display(Name = "非簽約經銷商(C類)")] 非簽約經銷商 = 2, [Display(Name = "個人")] 個人 = 3, [Display(Name = "自有倉庫")] 自有倉庫 = 4, [Display(Name = "其他")] 其他 = 5, }

C# DropDownList 綁定枚舉類