繫結Enum到DropDownList控制項的方法

2021-05-24 02:33:22 字數 560 閱讀 9663

有時,需要將乙個列舉型別繫結到dropdownlist供使用者選擇,這主要是enum.getnames和enum.getvalues的使用。

方法如下:

dropdownlist.datasource = enum.getnames(typeof(yourenumtype));

dropdownlist.databind();

這樣,可以直接將enum繫結到dropdownlist控制項上,不過有個缺點是,dropdownlist的value也是列舉的名稱而不是列舉代表的數值,有時,我是需要列舉數值的,因為一般以數值方式儲存到資料庫。這時可以使用下面的方法:

string names = enum.getnames(typeof(yourenumtype));

int values = (int)enum.getvalues(typeof(yourenumtype));

for (int i = 0; i < names.length; i++)

dropdownlist.items.add(new listitem(names[i], values[i].tostring()));

關於dropdown呼叫XML 繫結資料

dripdown 呼叫 xml中的資料 在web層中 xml資料夾新增 facilitytype.xml 如下 在model層中新增facilitytype1entity.cs 如下 using system using system.collections.generic using system...

wpf中列舉Enum型別繫結問題

正常情況列舉型別是不能繫結顯示的,所以需要乙個轉換器將列舉轉為字串才能正常在控制項裡顯示 具體 如下 轉換器 列舉轉字串 valueconversion typeof enum typeof string public class enumtostringconverter ivalueconver...

繫結資料到ListView

listview1.items.clear 刪除所有元素 listview1.items.remove listview1.selecteditems 0 刪除行 code private void button1 click object sender,eventargs e 取值賦值 listv...