【问题标题】:Why changing SelectedItem in one Combo changes all other Combos?为什么在一个 Combo 中更改 SelectedItem 会更改所有其他 Combo?
【发布时间】:2018-02-28 05:32:26
【问题描述】:

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

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 中都会更改?

【问题讨论】:

    标签: c# winforms combobox


    【解决方案1】:

    将它们分别绑定到 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";
        }
    }
    

    【讨论】:

    • 谢谢大师。解决了。​​
    • 根据 ComboBox 的数量和 DataTable 中的数据量,这可能会由于数据的重复而导致应用程序的内存占用大量增加。
    • @cadrell0 - 好点子,我没有考虑过这一点,并认为它只适用于几个组合框。
    • 下面的回答link,表示不需要额外的内存。 cmbApprover1.BindingContext = new BindingContext();
    【解决方案2】:

    更好的方法是使用DataView 来避免重复数据。另外,如果可以避免,请不要多次施放。

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

    编辑

    我刚刚意识到您可以使用 LINQ 更简洁地做到这一点

    foreach (ComboBox comboBox in this.Controls.OfType<ComboBox>())
    {
        comboBox.DataSource = new DataView(DataSet1.Tables[0]);
        comboBox.DisplayMember = "Articles";
    }
    

    【讨论】:

    • 这是一种更好的方法。接受的答案会占用更大的内存。
    【解决方案3】:

    我遇到了同样的问题,但我使用的是泛型。我已经使用组合框的绑定上下文来摆脱它。 (当您不知道绑定列表的大小时非常有用 - 在您的情况下它是 5 个项目)

    在下面的代码中,DisplayBindItem 只是一个具有 Key 和 Value 的类。

        List<DisplayBindItem> cust = (from x in _db.m01_customers
                where x.m01_customer_type == CustomerType.Member.GetHashCode()
                select new DisplayBindItem
                {
                    Key = x.m01_id.ToString(),
                    Value = x.m01_customer_name
                }).ToList();
    
        cmbApprover1.BindingContext = new BindingContext();
        cmbApprover1.DataSource = cust;
        cmbApprover1.DisplayMember = "Value";
        cmbApprover1.ValueMember = "Key";
    
        //This does the trick :)
        cmbApprover2.BindingContext = new BindingContext();
        cmbApprover2.DataSource = cust ;
        cmbApprover2.DisplayMember = "Value";
        cmbApprover2.ValueMember = "Key";
    

    供您参考的类。

        public class DisplayBindItem
        {
            private string key = string.Empty;
    
        public string Key
        {
            get { return key; }
            set { key = value; }
        }
        private string value = string.Empty;
    
        public string Value
        {
            get { return this.value; }
            set { this.value = value; }
        }
    
        public DisplayBindItem(string k, string val)
        {
            this.key = k;
            this.value = val;
        }
    
        public DisplayBindItem()
        { }
    }
    

    如果这解决了您的问题,请标记为答案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-30
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      相关资源
      最近更新 更多