public class yourItem
{
public int id;
public string name;
public yourItem(int id,string name)
{
this.id = id;
this.name = name;
}
public override ToString()
{
return this.name;
}
}yourItem item = new yourItem(1,"11");
combobox.items.add(item);((item)combobox.selectitem).id 就是你要的
{
public int id;
public string name;
public yourItem(int id,string name)
{
this.id = id;
this.name = name;
}
public override ToString()
{
return this.name;
}
}yourItem item = new yourItem(1,"11");
combobox.items.add(item);((item)combobox.selectitem).id 就是你要的
item = new ListItem("姓名","编号");
ComboBox.Items.Add(item);
我是winform程序,好象没有ListItem类
以前,我做的也是这样的
但是,下拉时怎么显示为“编号” + “姓名”
而不是显示为“命名空间” + “类名”;
当然,combobox.items.add(obj);obj必须为object型的
//先事例化yourtable,yourdataSet。
combobox.DataSource = "yourtable";
combobox.DisplayMember = "yourtable.编号";
combobox.ValueMember = "yourtable.姓名";
combobox.DataBindings.Add("SelectedValue", yourdataSet,"yourtable.编号");(CurrencyManager) BindingContext["yourdataSet", "yourtable"].AddNew();;
{
private string id = "";
private string name = "";
public ComboItem(string sid,string sname)
{
id = sid;
name = sname;
}
public string ID
{
get{return this.id;}
set{this.id = value;}
}
public string Name
{
get{ return this.name;}
set{this.name = value;}
}
}foreach(DataRow row in ds.Tables["WFBillState"].Rows)
{
ComboItem item=new ComboItem(row["WFStateID"].ToString(),row["WFStateName"].ToString());
this.tbx_BillState.Items.Add(item);
this.tbx_BillState.DisplayMember = "Name";
this.tbx_BillState.ValueMember = "ID";
}
是将数据加入
解决了
自己写了个SearchComboBox控件
那么:把选中的项转化为我们定义的类item((item)combobox.selectitem).id 就是你要的
Hexudong1979(何须懂何必懂)因为combobox的Item.ADD(一个任意类型的变量)
而显示的时候调用的是这个变量的ToString()方法
如果这个类没有重载ToString()
那么显示的结果就是命名空间 + 类名
我在一楼的回答
重写了ToString();
public override ToString()
{
return this.name;
}
所以这样显示的就是name这个字段了
{
if(item.ID==dg[dg.CurrentRowIndex,5].ToString())
{
int num=this.tbx_BillState.FindStringExact(item.Name,0);
this.tbx_BillState.SelectedIndex=num;
break;
}
}