今天WINFROM编程遇到这么一个问题:是有关WINFORM中两个comboBox控件绑定同一个数据源的问题,在窗体的界面上有两个comboBox,我在Form1_Load中对他们做了数据绑定(具体代码如下),但是运行出现了这个一个奇怪的问题,我改变了comboBox1的值,但是comboBox2的值也自动改变了,我没有设置comboBox1_SelectedIndexChanged函数
private void Form1_Load(object sender, EventArgs e)
{
dataset ds=getds("select code,name from shp") ;
comboBox1.DataSource = ds;
comboBox1.ValueMember = "code";
comboBox1.DisplayMember = "name";
comboBox1.SelectedIndex = 0;
comboBox2.DataSource = ds;
comboBox2.ValueMember = "code";
comboBox2.DisplayMember = "name";
comboBox2.SelectedIndex = 0;
}
解决方法:
dataset ds=getds("select code,name from shp")
comboBox1.DataSource = ds;
comboBox1.ValueMember = "code";
comboBox1.DisplayMember = "name";
comboBox1.SelectedIndex = 0;
comboBox2.DataSource = ds.Copy();
comboBox2.ValueMember = "code";
comboBox2.DisplayMember = "name";
comboBox2.SelectedIndex = 0;