简单问题:combobox 如何把选中的项的value值传到textbox中 如题,如combobox中有下列项目value text 1 语文 2 数学 3 英语当选择combobox中语文时,textboxd的值变为1该如何做啊? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你的Combobox是绑定的数据库吗中的数据吗? WebForm无此问题,在Winform中如果是绑定数据源,就将DisplayMember和ValueMember分别对应为Text和Value,如果是通过Add方法将一般对象添加到列表中,则重写对象的ToString方法,显示的Text就是这个方法的返回值,Value应是对象的某个属性。 comboBox.SelectedIndexcomboBox.SelectedValue 给你写个绑定的例子:建数据库:create database student;use student;create table Item( id int, name varchar(20))insert into Item values(1,'语文');insert into Item values(2,'数学');insert into Item values(3,'英语');代码://引入using System.Data.SqlClient;操作代码:private void Form1_Load(object sender, EventArgs e) { DataBind(); } //数据绑定 private void DataBind() { SqlConnection con = new SqlConnection("server=.;database=student;uid=sa;pwd=0421"); SqlDataAdapter sda = new SqlDataAdapter("select * from Item", con); DataSet ds = new DataSet(); sda.Fill(ds, "Item"); this.comboBox1.DataSource = ds.Tables["Item"]; this.comboBox1.DisplayMember = "name"; this.comboBox1.ValueMember = "id"; } private void comboBox1_SelectedValueChanged(object sender, EventArgs e) { if (this.comboBox1.SelectedItem != null) { this.textBox1.Text = this.comboBox1.SelectedValue.ToString(); } } private void comboBox1_SelectedItemChanged(object sender, EventArgs e){this.textBox1.Text = this.comboBox1.SelectedIndex.ToString();} 请详细解释一下下面的代码 C#--操作注册表--提示“未将对象引用设置到对象的实例” 为什么这么多教材把冒泡排序写错那? WPF打印问题 怎样访问需要身份验证的局域网里的某个目录? 两个页面之间搜索的问题 如何连接远程服务器啊?(Oracle,数据库不在本地机器上,在服务器上) 菜鸟提问,给分 Linq 问题?? 如何获得文本框插入符位置? 信念是支撑程序员走下去的力量源泉 100求助各位大侠,怎样用正则表达式对比字符串
comboBox.SelectedValue
create database student;use student;create table Item
( id int,
name varchar(20)
)
insert into Item values(1,'语文');
insert into Item values(2,'数学');
insert into Item values(3,'英语');代码:
//引入
using System.Data.SqlClient;操作代码:
private void Form1_Load(object sender, EventArgs e)
{
DataBind();
}
//数据绑定
private void DataBind()
{
SqlConnection con = new SqlConnection("server=.;database=student;uid=sa;pwd=0421");
SqlDataAdapter sda = new SqlDataAdapter("select * from Item", con);
DataSet ds = new DataSet();
sda.Fill(ds, "Item");
this.comboBox1.DataSource = ds.Tables["Item"];
this.comboBox1.DisplayMember = "name";
this.comboBox1.ValueMember = "id";
}
private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
{
if (this.comboBox1.SelectedItem != null)
{
this.textBox1.Text = this.comboBox1.SelectedValue.ToString();
}
}
{
this.textBox1.Text = this.comboBox1.SelectedIndex.ToString();
}