Winfrom c# 两个ComboBox互相联动

第一次写winform程序遇到一个头疼到问题就是两个comboBox互相联动绑定,开始用comboBox1_SelectedIndexChanged事件,结果绑定comboBox1的时候,就执行comboBox1_SelectedIndexChanged这个事件。很头疼。

后来发现微软还提供了一个comboBox1_DropDownClosed事件:当关闭组合框下拉部分时发生,当用程序改变SelectedIndex时就不会出现两个comboBox相互改对方到Index并触发comboBox1_SelectedIndexChanged,

后来将SelectedIndexChanged事件全部换成DropDownClosed,就可以实现两个comboBox互相联动了;

TextChanged事件和TextUpdate也是一个对用程序改变其值敏感,一个不敏感。

代码贴出来:

 private void ComboBox_Load()
        {
            Maticsoft.BLL.Province_BLL pvb = new Maticsoft.BLL.Province_BLL();
            //this.comboBox1.DataSource = pvb.GetAllList().Tables[0];
            //this.comboBox1.DataSource =pvb.GetList_DropDownList("",500);
            this.comboBox1.DisplayMember = "Province";
            this.comboBox1.ValueMember = "ID";
            this.comboBox1.DataSource = pvb.GetList_DropDownList("", 500).Tables[0].DefaultView;
           // this.comboBox1.SelectedIndex = -1;
        }
        private void FormBtownArea_Load(object sender, EventArgs e)
        {
          
                ComboBox_Load();
            
        }
        private void comboBox1_DropDownClosed(object sender, EventArgs e)
        {
            try
            {
                //this.CBB_Town.Items.Clear();
                Maticsoft.BLL.Downtown_BLL db = new Maticsoft.BLL.Downtown_BLL();

                this.CBB_Town.DisplayMember = "Downtown";
                this.CBB_Town.ValueMember = "ID";
                this.CBB_Town.DataSource = db.GetList_DropDownList("ProvinceID=" + this.comboBox1.SelectedValue.ToString()).Tables[0].DefaultView;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            } 
        }


你可能感兴趣的:(Winfrom c# 两个ComboBox互相联动)