绑定数据源
第一步肯定是在页面拖个checkbox控件,就叫cmbValue吧;
第二步创建一个类:
public class ListItem
{
public string Key { get; set; }
public string Value { get; set; }
public ListItem(string strKey, string strValue)
{
this.Key = strKey;
this.Value = strValue;
}
}
第三步创建数组然后添加对象:
ArrayList lists = new ArrayList();
lists.Add(new ListItem("1", "男"));
lists.Add(new ListItem("0", "女"));
第四步给控件赋值:
this.cmbValue.ValueMember = "Key";
this.cmbValue.DisplayMember = "Value";
this.cmbValue.DataSource = lists;
OK,就可以了
image.png
获取值
在之间创建的那个类里面加个方法:
//根据ListItem中的Value找到特定的ListItem
public static ListItem FindByValue(ComboBox cmb, string strValue)
{
foreach (ListItem li in cmb.Items)
{
if (li.Value == strValue)
{
return li;
}
}
return null;
}
然后通过显示值获取实际值:
ListItem li = ListItem.FindByValue(cmbValue, Value); //根据Value获得Key
string ID = li.Key; //实际列名