private void Form1_Load(object sender, EventArgs e)
{
string connstr = "server=(local);uid=sa;pwd=ccxx;database=master";
string sql = "select id,班级 from 班级1";
using (SqlConnection conn = new SqlConnection(connstr))
{
try
{
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(sql, conn); DataTable dt = new DataTable();
da.Fill(dt); comboBox1.DataSource = dt;
comboBox1.DisplayMember = "班级";
comboBox1.ValueMember = "id";
}
catch (Exception ex)
{ throw new Exception(ex.Message);
}
}
} private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string selectindex = comboBox1.SelectedValue.ToString();
textBox1.Text = selectindex;
} private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
//string selectindex = comboBox2.SelectedItem.ToString();
//textBox1.Text = selectindex;
}
}简单说一下 如果用selectvalue 能得到combox的valuenumber combobox2是写死的 用
selectitem可以显示在文本框 combobox1是从数据源获取的 用selectitem 显示的东西是System.Data.DataRowView
{
string connstr = "server=(local);uid=sa;pwd=ccxx;database=master";
string sql = "select id,班级 from 班级1";
using (SqlConnection conn = new SqlConnection(connstr))
{
try
{
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(sql, conn); DataTable dt = new DataTable();
da.Fill(dt); comboBox1.DataSource = dt;
comboBox1.DisplayMember = "班级";
comboBox1.ValueMember = "id";
}
catch (Exception ex)
{ throw new Exception(ex.Message);
}
}
} private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string selectindex = comboBox1.SelectedValue.ToString();
textBox1.Text = selectindex;
} private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
//string selectindex = comboBox2.SelectedItem.ToString();
//textBox1.Text = selectindex;
}
}简单说一下 如果用selectvalue 能得到combox的valuenumber combobox2是写死的 用
selectitem可以显示在文本框 combobox1是从数据源获取的 用selectitem 显示的东西是System.Data.DataRowView
解决方案 »
- 为什么 IBatis.net 会把 statements id 当成存储过程名?
- 求教,cmdGetFunctionName.ExecuteScalar 执行后抛出异常"Connection 属性尚未初始化。"
- 【C#必备知识点】 添加现有解决方案
- 那位有PluginInstaller.msi,网上怎么就找不到呢?
- 关于多线程
- winform有缓存吗?
- Datagridview中column.Items..(为什么没有selectIndex,和selectValue方法啊,应该怎么取值和设置其默认的值啊)
- 请教Marshal的问题
- asp.net datagrid取当前行传值
- 为什么我用Icon.Save保存下来的图标会失真?
- Windows Mobile + SQL Server開發問題
- 请教一个关于sqlite数据库的 基础问题
textBox1.Text = drv["班级"].ToString()