关于combobox的一个问题

tanweish 2005-04-07 11:44:03
我有一个表:
例如产品类型表,字段有:
类型ID,类型名称

在输入产品时,产品类型用COMBOBOX进行选择,在初始化COMBOBOX时,我是把类型名称填进去的,但是要把产品更新到数据库时,我需把类型ID更新进产品表,我要重新从产品类型表中找出对应的产品类型ID吗?
...全文
99 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
mmqingfeng 2005-04-08
  • 打赏
  • 举报
回复
combox的item是object类型,具体显示的内容是通过ToString()方法得到的字符串。
所以你可以建一个“产品”类,并重写它的ToString()方法

class MyData
{
public string id;
public string name;

public override string ToString()
{
return name;
}
}

// 插入数据
MyData data = new MyData();
data.id = "123";
data.name = "aaa";
comboBox1.Items.Add(data);


// 取得id
MyData data = comboBox1.Items[index] as MyData;
if(data!=null)
{
id = data.id;
}
凨叔 2005-04-08
  • 打赏
  • 举报
回复
你用数据绑定呀。
comboBox.DataSource = [DataTable];
comboBox.DiaplayMember = "ID";
comboBox.ValueMember = "Type";
syfsz 2005-04-08
  • 打赏
  • 举报
回复
public class ItemVO : System.Object
{
public string id;
public string desc;

public ItemVO(string Id, string Desc)
{
this.id = tableId;
this.desc = tableDesc;
}

public override string ToString()
{
return this.desc;
}

public override bool Equals(System.Object obj)
{
if (this.GetType().Equals(obj.GetType()))
{
ItemVO that=(ItemVO) obj;
return (this.id.Equals(that.id));
}
return false;
}
public override int GetHashCode()
{
return this.id.GetHashCode();;
}
}

================
comboBox1.Items.Add(new ItemVO(id,name));
((ItemVO)comboBox1.SelectedItem).id
((ItemVO)comboBox1.SelectedItem).desc
新鲜鱼排 2005-04-07
  • 打赏
  • 举报
回复
需要,要实现对应的关系吧。

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧