綁定數據源
第一步肯定是在頁面拖個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; //實際列名