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
textBox1.Text = drv["班级"].ToString()