为什么在一个 Combo 中更改 SelectedItem 会更改所有其他 Combo? [英] Why changing SelectedItem in one Combo changes all other Combos?

查看:12
本文介绍了为什么在一个 Combo 中更改 SelectedItem 会更改所有其他 Combo?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以这种方式填充了组合框

I populated comboboxes in this way

foreach (Control c in this.Controls)
{
     if (c is ComboBox)
     {
         (c as ComboBox).DataSource = DataSet1.Tables[0];
         (c as ComboBox).DisplayMember = "Articles";
     }
}

但是,问题是当我在一个 Combo 中更改 SelectedItem 时 - 它会在所有其他 Combo 中更改吗?

But, problem is when I change SelectedItem in one Combo - it becomes changed in all other Combos?

推荐答案

将它们各自绑定到 DataSet1.Table[0] 的单独实例.

Bind them each to a separate instance of the DataSet1.Table[0].

即)

foreach (Control c in this.Controls)
{
    if (c is ComboBox)
    {
        DataTable dtTemp = DataSet1.Tables[0].Copy();
        (c as ComboBox).DataSource = dtTemp 
        (c as ComboBox).DisplayMember = "Articles";
    }
}

这篇关于为什么在一个 Combo 中更改 SelectedItem 会更改所有其他 Combo?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆