您的位置:首页 > 编程语言 > C#

C#遍历枚举,并绑定至类似DropDownList的控件上。

2010-07-17 10:30 483 查看
一、枚举

#region GroupGrade:用户组等级
///
/// 用户组等级
///
public enum GroupGrade
{
///
/// 一级
///
一级 = 1,
///
/// 二级
///
二级 = 2,
///
/// 三级
///
三级 = 3,
///
/// 四级
///
四级 = 4,
///
/// 五级
///
五级 = 5,
///
/// 六级
///
六级 = 6
}
#endregion


二、实现绑定的方法

///
/// 对DropDownList对象执行数据绑定
///
/// 要绑定的DropDownList对象 /// 枚举类型 /// 默认值 /// 第一项提示字符 public static void DropDownList(ref DropDownList select, Type type, string value, string prompt)
{
select.Items.Clear();
if (prompt != null && prompt != "")
{
ListItem dl = new ListItem(prompt, "");
select.Items.Add(dl);
}
if (type != null)
{
int i = 0;
string[] names = Enum.GetNames(type);
foreach (int v in Enum.GetValues(type))
{
ListItem li = new ListItem(names, v.ToString());
if (value == v.ToString()) li.Selected = true;
select.Items.Add(li);
}
}
}


三、使用示例
DropDownList(ref DropDownList1, typeof(GroupGrade), null, "选择等级");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: