参考文章:
1,http://topic.csdn.net/u/20091027/16/b804ebc8-c88c-4c05-9652-d198d4b4f0fb.html
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(conn);
con.Open();
SqlCommand cmd = new SqlCommand("select * from department",con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
con.Close();
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "department";
comboBox1.ValueMember = "departmentId";
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex>-1)
{
DataRowView drv = (DataRowView)comboBox1.SelectedItem;
int id = Convert.ToInt32(drv.Row["departmentId"].ToString());
SqlConnection con = new SqlConnection(conn);
con.Open();
SqlCommand cmd = new SqlCommand("select * from major where departmentId='" + id + "'", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
comboBox2.DataSource = dt;
comboBox2.DisplayMember = "major";
comboBox2.ValueMember = "majorId";
}
2,
http://blog.csdn.net/gaofeng2000/archive/2009/05/06/4156247.aspx
ComboBox的SelectionChangeCommitted事件
实现方案:
在ComboBox的SelectionChangeCommitted事件 写 联动取数:
private void comBox_subject_SelectionChangeCommitted(object sender, System.EventArgs e)
{
if (comBox_subject.SelectedIndex > -1)
{
DataRowView drv = (DataRowView)comBox_subject.SelectedItem;
String check_subject = drv.Row["学科"].ToString();
string connectionStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + System.Windows.Forms.Application.StartupPath + "/teachdb.mdb";
OleDbConnection conn = new OleDbConnection(connectionStr);
conn.Open();
string strsubject = "select distinct 出版社 from presstable where 学科='" + check_subject + "'";
OleDbDataAdapter MyAdapter1 = new OleDbDataAdapter(strsubject, conn);
DataSet ds = new DataSet();
MyAdapter1.Fill(ds, "press");
comBox_Press.DataSource = ds.Tables["press"].DefaultView;
comBox_Press.DisplayMember = "出版社";
comBox_Press.ValueMember = "出版社";
conn.Close();
}
}
3
http://www.cnblogs.com/luoht/archive/2010/02/25/1673737.html
今天工作中遇到一个头疼到问题就是两个comboBox互相联动绑定,开始用comboBox1_SelectedIndexChanged事件,结果两个comboBox不停的相互绑定,死循环了
后来发现微软还提供了一个comboBox1_DropDownClosed事件:当关闭组合框下拉部分时发生,当用程序改变SelectedIndex时就不会出现两个comboBox相互改对方到Index并触发comboBox1_SelectedIndexChanged,
后来将SelectedIndexChanged事件全部换成DropDownClosed,就可以实现两个comboBox互相联动了;
TextChanged事件和TextUpdate也是一个对用程序改变其值敏感,一个不敏感。