枚举:
/// <summary>
/// 设备使用状态 0:在用 1:停用
/// </summary>
public enum DeviceUseStatus
{
/// <summary>
/// 在用
/// </summary>
[Remark("运行中")]
Running = 0,
/// <summary>
/// 停用
/// </summary>
[Remark("停用")]
Stoped = 1
}
获取枚举方法:
/// <summary>
/// 获取值,名称、及备注
/// </summary>
/// <param name="enumType"></param>
/// <returns></returns>
public static List<ValueNameText> GetEnumValueNameTextCollection(Type enumType)
{
List<ValueNameText> list = new List<ValueNameText>();
Array array = Enum.GetValues(enumType);
foreach (object o in array)
{
ValueNameText vnt = new ValueNameText(Convert.ToInt32(o), Enum.GetName(enumType, o), RemarkAttribute.GetRemark(o));
list.Add(vnt);
}
return list;
}
枚举类 ValueNameText
/// <summary>
/// 值名称备注
/// </summary>
[Serializable]
public class ValueNameText
{
public object Value { get; set; }
public string Name { get; set; }
public string Text { get; set; }
public ValueNameText(object obj, string name, string memo)
{
this.Value = obj;
this.Name = name;
this.Text = memo;
}
}
操作:
/// <summary>
/// 绑定条件控件
/// </summary>
private void BindControl()
{
List<ValueNameText> listDate = GetEnumValueNameTextCollection(typeof(DeviceUseStatus));
ddl_Statue.DataTextField = "Text";
ddl_Statue.DataValueField = "Value";
ddl_Statue.DataSource = listDate ;
ddl_Statue.DataBind();
}