.Net平台Winform两个ComboBox控件绑定同一个数据源

今天WINFROM编程遇到这么一个问题:是有关WINFORM中两个comboBox控件绑定同一个数据源的问题,在窗体的界面上有两个comboBox,我在Form1_Load中对他们做了数据绑定(具体代码如下),但是运行出现了这个一个奇怪的问题,我改变了comboBox1的值,但是comboBox2的值也自动改变了,我没有设置comboBox1_SelectedIndexChanged函数

 1 private void Form1_Load(object sender, EventArgs e) 
 2 { 
 3 dataset ds=getds("select code,name from shp") ;
 4  
 5 comboBox1.DataSource = ds; 
 6 comboBox1.ValueMember = "code"; 
 7 comboBox1.DisplayMember = "name"; 
 8 comboBox1.SelectedIndex = 0; 
 9 
10 
11 comboBox2.DataSource = ds; 
12 comboBox2.ValueMember = "code"; 
13 comboBox2.DisplayMember = "name"; 
14 comboBox2.SelectedIndex = 0; 
15 }

 

解决方法:

 1 dataset ds=getds("select code,name from shp") 
 2 
 3 comboBox1.DataSource = ds; 
 4 comboBox1.ValueMember = "code"; 
 5 comboBox1.DisplayMember = "name"; 
 6 comboBox1.SelectedIndex = 0; 
 7 
 8 comboBox2.DataSource = ds.Copy(); 
 9 comboBox2.ValueMember = "code"; 
10 comboBox2.DisplayMember = "name"; 
11 comboBox2.SelectedIndex = 0;

 

posted @ 2014-03-03 15:06  银河系上的地球  阅读(606)  评论(0编辑  收藏  举报