最近有点收获,写下来,以后忘了还能查查,嘿嘿。第一次写,小菜啊,请大家多多指正啊。
1. 我们都知道 SelectedValueChanged和SelectedIndexChanged在绑定数据经常会被触发。这篇写的很详细:
http://www.cnblogs.com/gossip/archive/2008/12/10/1352193.html
2. 我发现用这样的方法可以避免多次触发:
1 public void BindUsers() 2 { 3 // 解除SelectedIndexChanged事件 4 this.comboBox1.SelectedIndexChanged -= new System.EventHandler(this.comboBox1_SelectedValeChanged); 5 this.comboBox1.DataSource = GetProvince(); 6 this.comboBox1.ValueMember = "Id"; 7 this.comboBox1.DisplayMember = "Name"; 8 // 重新绑定SelectedIndexChanged事件 9 this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedValeChanged); 10 }
3. 最近,做winform的一个小东西中,小小的利用了这个特征。
以下是我想要在页面加载完成时实现的效果,combox1加载完毕显示省份,combox2完成了城市的绑定。
一般的做法是在加载时绑定省份,而后在绑定当前省份对应的城市。
我采用了下面的做法,非常简单,呵呵。
1 public void BindProvince() 2 { 3 // 解除SelectedIndexChanged事件 4 //this.comboBox1.SelectedIndexChanged -= new System.EventHandler(this.comboBox1_SelectedValeChanged); 5 this.comboBox1.ValueMember = "ProvinceId"; 6 this.comboBox1.DataSource = GetProvince(); 7 this.comboBox1.DisplayMember = "ProvinceName"; 8 // 重新绑定SelectedIndexChanged事件 9 //this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedValeChanged); 10 }
先绑 ProvinceId 属性,再绑定数据源,这样在执行完 this.comboBox1.DataSource = GetProvince(); 后,会触发 SelectedIndexChanged 事件,这就自动的进行了城市数据的绑定。
写的不是很好,就当是抛砖引玉吧。