控件LookUpEdit(下拉菜单)
分为两种
1.数据源来源为枚举型
public enum EWarehouseState
{
[CommentAttribute("已删除")]
deleted = -1,
[CommentAttribute("未使用")]
Unused = 0,
[CommentAttribute("已锁定")]
Locked = 1,
[CommentAttribute("已出库")]
OutoftheWarehouse = 2,
[CommentAttribute("已使用")]
Used = 3
}
//状态栏的下拉
lookUpStatus.Properties.DataSource = EnumHelper.GetAllEnumKeyValue<EWarehouseState>();
lookUpStatus.Properties.DisplayMember = "Value";
lookUpStatus.Properties.ValueMember = "Key";
//获取枚举的所有值
public static List<KeyValuePair<int, string>> GetAllEnumKeyValue<T>()
{
List<KeyValuePair<int, string>> lstResult = new List<KeyValuePair<int, string>>();
var enumType = typeof(T);
foreach (var item in Enum.GetNames(enumType))
{
var attrs = enumType.GetField(item).GetCustomAttributes(false);
CommentAttribute commentAttr;
KeyValuePair<int, string> keyValueItem;
var value = (int)enumType.GetField(item).GetValue(enumType);
if (attrs != null && (commentAttr = attrs.FirstOrDefault(p => p is CommentAttribute) as CommentAttribute) != null)
{
keyValueItem= new KeyValuePair<int, string>(value, commentAttr.Commment);
}
else
{
keyValueItem = new KeyValuePair<int, string>(value, item);
}
lstResult.Add(keyValueItem);
}
return lstResult;
}
2.数据来源为数据库
lstMachine = JsonHelper.DeserializeObject<List<Machinemodel>>(AppSession.WindowsDataService.GetMachineModels(JsonHelper.SerializeObject(null)));//从数据库中查询数据
lookUpMachineModel.Properties.DataSource = lstMachine;//绑数据源
lookUpMachineModel.Properties.DisplayMember = "Modelname";//设置显示的字段
lookUpMachineModel.Properties.ValueMember = "Id";
如果下拉菜单显示和绑定的数据不一致,检查Designer中的代码