winform的ComboBox中只能赋值text,显示和值是同样的,不少时候不能知足根本须要,熟悉B/S开发的coder最经常使用的就是text和value分开的,并且web下DropDownList原本就是分为text和value。ComboBox要实现一样功能,使item有多个值,只能用重写一个类来实现了。html
重写类以下:web
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; } } }
绑定数据时:ide
bubufxComboBox.Items.Add(new cListItem(drv["bubufx_text"].ToString(), drv["ID"].ToString()));
取值时:this
string bubufxComboBox_str = ((cListItem)bubufxComboBox.SelectedItem).ID;
bubufx分享,禁止转载。原文:【winform中ComboBox实现text和value,使显示和值分开,重写text和value属性】spa