winform的ComboBox中只能賦值text,顯示和值是一樣的,很多時候不能滿足根本需要,熟悉B/S開發的coder最常用的就是text和value分開的,而且web下DropDownList本來就是分為text和value。ComboBox要實現同樣功能,使item有多個值,只能用重寫一個類來實現了。
重寫類如下:
using System;
namespace sm
{
class cListItem
{
private string id = string.Empty;
public string ID
{
get { return id; }
set { id = value; }
}
private string name = string.Empty;
public string Name
{
get { return name; }
set { name = value; }
}
public cListItem(string name, string id)
{
this.id = id;
this.name = name;
}
public override string ToString()
{
return this.name;
}
}
}
綁定數據時:
bubufxComboBox.Items.Add(new cListItem(drv["bubufx_text"].ToString(), drv["ID"].ToString()));
取值時:
string bubufxComboBox_str = ((cListItem)bubufxComboBox.SelectedItem).ID;
bubufx分享,禁止轉載。原文:【winform中ComboBox實現text和value,使顯示和值分開,重寫text和value屬性】
