winform中给combobox赋值问题,很急,在线等

g505149841 2010-07-09 09:15:12
我现在想给combobox绑定数据,我想在前台显示中文,后台调用的时候取英文,就好像,我有2组值,北京,beijin;浙江,zhejiang我想把这2个值都绑定到combobox中,在前台combobox的下拉列表里显示北京,浙江。在后台代码中,我在操作数据库的时候就用beijin,zhejiang。请问怎么实现
...全文
2248 36 打赏 收藏 转发到动态 举报
写回复
用AI写文章
36 条回复
切换为时间正序
请发表友善的回复…
发表回复
sdnjzhangxia 2012-08-24
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 的回复:]
List<KeyValuePair<object, string>> dit = new List<KeyValuePair<object, string>>();
dit.Add(new KeyValuePair<object, string>(1, "厦门"));
dit.Add(new KeyValuePair<object, string>(2, "上海"));

th……
[/Quote]
这个非常好,我做出来了,谢谢
g505149841 2010-07-10
  • 打赏
  • 举报
回复
问题解决了,昨天忘了结贴。。在此谢谢各位大侠帮忙解答了。。。
小夏天~ 2010-07-09
  • 打赏
  • 举报
回复

// 自己添加的数据就把 ds.Tables[0] 换成你的那个表名就OK.

小夏天~ 2010-07-09
  • 打赏
  • 举报
回复

//存值
cmb.DataSource=ds.Tables[0];
cmb.DisplayMember="chinese"; //设置前台值,就是你要的中文放在chinese列
cmb.ValueMember="English"; //设置后台值,就是你要的中文放在English列


//取前台值
String chinese = cmb.Text;
//取后台值
String english = cmb.Value;

一切为了你 2010-07-09
  • 打赏
  • 举报
回复
this.ComboBox1.SelectedValue就能得到了你选中的那个值!!
一切为了你 2010-07-09
  • 打赏
  • 举报
回复
winform中不能按你要求实现 绝对
kj289907795 2010-07-09
  • 打赏
  • 举报
回复
大大滴 坏人!!楼上
zzyhuian06142 2010-07-09
  • 打赏
  • 举报
回复
KeyValuePair是这个,我的代码是手动写的啦,写错单词le ,哈哈
g505149841 2010-07-09
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 zzyhuian06142 的回复:]
public class TestInfo
{
string _strName;
object _objValue;
public string GetName
{
get { return _strName; }
set { this._strName = value; }
}


先定义这个类
然后
Dictionary<string,……
[/Quote]

请问Foreach(KeyValuePare<string,string> s in d)里面的KeyValuePare是什么意思,我程序运行时报错说找不到型別或命名空間名稱 'keyvaluepare' (您是否遺漏 using 指示詞或組件參考?)
kj289907795 2010-07-09
  • 打赏
  • 举报
回复
觉的行不通
kj289907795 2010-07-09
  • 打赏
  • 举报
回复
这个 确实可以实现吗???
zzyhuian06142 2010-07-09
  • 打赏
  • 举报
回复
public class TestInfo
{
string _strName;
object _objValue;
public string GetName
{
get { return _strName; }
set { this._strName = value; }
}


先定义这个类
然后
Dictionary<string,string> d = new Dictionary<string,string>();
d.add("HHAA000000AAAC00","人事課");//后面一样的加
List<TestInfo> t = new List<TestInfo>();
最后
Foreach(KeyValuePare<string,string> s in d)
{
TestInfo ti = new TestInfo();//就是上面的那个类
ti.GetName = s.Value;
ti.GetValue = s.Key;
t.add(ti);
}

this.comboBox1.DataSource = t;
this.comboBox1.ValueMember = "GetValue";
this.comboBox1.DisplayMember = "GetName";


最后 ,如果你要得到对应的编码的话
this.ComboBox1.SelectedValue就能得到了

g505149841 2010-07-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wuyq11 的回复:]
cmb.DataSource=ds.Tables[0];
cmb.DisplayMember="NAME";
cmb.ValueMember="ID";
[/Quote]
请问tables怎么来,我的数据时自己添加的!
zhengqian529 2010-07-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wuyq11 的回复:]
cmb.DataSource=ds.Tables[0];
cmb.DisplayMember="NAME";
cmb.ValueMember="ID";
[/Quote]

正解啊
g505149841 2010-07-09
  • 打赏
  • 举报
回复
谢谢14#的回答,可惜我看不懂啊,我就是想把
前台显示项 后台相对应的编码
人事課 HHAA000000AAAC00
信息課 HHAA000000AAAB00
倉管課 HHAA0000ABACAC00
管理部 HHAA000000AA0000
这些数据都绑定到combobox1中,然后我像在下拉列表中显示
人事課
信息課
倉管課
管理部
然后我在程序运行的时候选择下列列表,选中那项,我在后台中相对应的取到我选择项的编码,怎么选择也帮忙写下,谢谢
zzyhuian06142 2010-07-09
  • 打赏
  • 举报
回复
你要加入using System.Collections.Generic;
g505149841 2010-07-09
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 jimmmmm 的回复:]
List<KeyValuePair<object, string>> dit = new List<KeyValuePair<object, string>>();
dit.Add(new KeyValuePair<object, string>(1, "厦门"));
dit.Add(new KeyValuePair<object, string>(2, "上海"));

th……
[/Quote]
请问list是什么,我这里怎么没有这项呢。。。
zzyhuian06142 2010-07-09
  • 打赏
  • 举报
回复
public class TestInfo
{
string _strName;
object _objValue;
public string GetName
{
get { return _strName; }
set { this._strName = value; }
}

public object GetValue { get { return _objValue; } set { _objValue = value; } }
}


//TestInfo ti = null;
//List<TestInfo> t = new List<TestInfo>();
//for (int i = 0; i < 5; i++)
//{
// ti = new TestInfo();
// ti.GetName = "Name:" + i.ToString();
// ti.GetValue = i;
// t.Add(ti);
//}


//this.dataGridView1.DataSource = t;
//this.comboBox1.DataSource = t;
//this.comboBox1.ValueMember = "GetValue";
//this.comboBox1.DisplayMember = "GetName";


这是个变通的方法
g505149841 2010-07-09
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 zzyhuian06142 的回复:]
引用 10 楼 g505149841 的回复:
引用 9 楼 zzyhuian06142 的回复:
不知道你的英文字段是否存在于数据库中的,如果是的话

ComboBox.DataSource=ds.Tables[0];
ComboBox.DisplayMember="Name_CN";
ComboBox.ValueMember="Name_EN";

如果不是的话,可以考虑根据中……
[/Quote]

我的想法就是你这样的,不过你给的代码里ComboBox1.DataSource = d;这句在运行时报错,说:
複雜 DataBinding 接受以 IList 或 IListSource 做為資料來源。
应该怎么解决啊,谢谢!
jimmmmm 2010-07-09
  • 打赏
  • 举报
回复
List<KeyValuePair<object, string>> dit = new List<KeyValuePair<object, string>>();
dit.Add(new KeyValuePair<object, string>(1, "厦门"));
dit.Add(new KeyValuePair<object, string>(2, "上海"));

this.comboBox1.DataSource = dit;
this.comboBox1.DisplayMember = "Value";
this.comboBox1.ValueMember = "Key";


这样也可以
加载更多回复(16)

110,533

社区成员

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

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

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