SelectedIndexChanged事件
C# code
private void Register_Load(object sender, EventArgs e)
{
try
{
DBHelper.conn.Open();
SqlCommand cmd = new SqlCommand("select distinct GradeName from Grade",DBHelper.conn);
SqlDataReader sdr=cmd.ExecuteReader();
while(sdr.Read())
{
comBGrade.Items.Add(sdr["GradeName"]);
}
sdr.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
DBHelper.conn.Close();
}
}
通过上面的代码已经实现了将数据库里的Grade信息附加到年级的ComboBox(comBGrade)框里,
现在不知道怎样实现 选中comBGrade索引里的一条内容,触发SelectedIndexChanged事件,
使Class的ComboBox中出现对应的班级
------解决方案--------------------楼主这应该是C/S的程序吧,combobox好久没用过了,应该是b/s里的dropdownlist
当选择comBGrade索引里的一条内容 生成SelectedIndexChanged事件,
在事件里写:
try
{
DBHelper.conn.Open();
SqlCommand cmd = new SqlCommand("select className from Class where Id=comBGradeId",DBHelper.conn);
SqlDataReader sdr=cmd.ExecuteReader();
while(sdr.Read())
{
combClass.Items.Add(sdr["className "]);
}
sdr.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
DBHelper.conn.Close();
}
把查询到的数据帮顶到要显示的Class里就行了。
------解决方案--------------------其实,说简单了,就是,只要lz再把上面那行sql语句改一下,改为从班级表中选择,后面加个"where grade=ComboBox.text",然后将选择到的数据加入另一个ComboBox就行了。。
------解决方案--------------------private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection("连接字符串"))
{
conn.Open();
SqlDataAdapter da = new SqlDataAdapter("select ClassName from Class inner join Grade on Class.GradeId=Grade.Id where Grade.GradeName=" + this.comboBox1.SelectedText, conn);
DataTable dt = new DataTable();
da.Fill(dt);
this.comboBox2.DataSource = dt;
this.comboBox2.DisplayMember = "ClassName";
}
}
// comboBox1是年级,comboBox2是班级