本文介绍了绑定组合框与文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好
i希望在combox中显示id及其在文本框中的值(意思是它的名字)
为此目的我的代码如下..
hello
i want to display id in combox and their value (means its name) in textbox
for that purpose i did bellow code..
string Query = "select id, name from LateFee";
cmd = new SqlCommand(Query, con);
//con.Open();
dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(string));
//dt.Columns.Add("name", typeof(string));
dt.Load(dr);
comboBox1.ValueMember = "id";
comboBox1.DisplayMember = "id";
comboBox1.DataSource = dt;
binding = new Binding("Text", comboBox1, "Text");
textBox1.DataBindings.Add(binding);
但是当我选择id时,在combox中讨价还价,在文本框中id只出现但我想要文本框名称应该出现。
我该怎么办???
but what is happing that in combox when i select id,in text box id is only appear but i want in textbox name should appear.
what should i do???
推荐答案
//dt.Columns.Add("name", typeof(string));
<<取消注释。
2)
<< uncomment that out.
2)
comboBox1.ValueMember = "id";
应为
comboBox1.ValueMember = "name";
3)我认为您需要更改绑定:
3)I think you need to change the binding from:
new Binding("Text", comboBox1, "Text");
to:
to:
new Binding("Text", comboBox1, "SelectedValue");
这篇关于绑定组合框与文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!