会员
周边
众包
新闻
博问
闪存
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
zyip
博客园
首页
新随笔
联系
订阅
管理
将enum绑定到dropdrownlist
Code
1
public
enum
SexEntity
2
{
3
Unknown
=-
1
,F
=
0
,M
=
1
4
}
5
6
7
public
static
DataTable GetAllSex()
8
{
9
SexEntity sex
=
new
SexEntity();
10
DataTable dt
=
new
DataTable();
11
dt.Columns.Add(
"
name
"
);
12
dt.Columns.Add(
"
value
"
);
13
DataRow dr
=
null
;
14
System.Reflection.FieldInfo[] fields
=
sex.GetType().GetFields();
15
string
strText
=
string
.Empty;
16
string
strValue
=
string
.Empty;
17
foreach
(FieldInfo field
in
fields)
18
{
19
if
(field.FieldType.IsEnum
==
true
)
20
{
21
strValue
=
((
int
)sex.GetType().InvokeMember(field.Name, BindingFlags.GetField,
null
,
null
,
null
)).ToString();
22
strText
=
field.Name;
23
dr
=
dt.NewRow();
24
dr[
"
name
"
]
=
strText;
25
dr[
"
value
"
]
=
strValue;
26
dt.Rows.Add(dr);
27
}
28
}
29
return
dt;
30
}
1
DropDownList1.DataSource
=
GetAllSex();
2
DropDownList1.DataTextField
=
"
name
"
;
3
DropDownList1.DataValueField
=
"
value
"
;
4
DropDownList1.DataBind();
posted @
2009-05-05 13:29
zyip
阅读(
321
) 评论(
4
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部
公告