【问题标题】:C# Windows Forms ComboBox's SelectionChangeCommitted event prevents selection change when Combobox's BindingSource property changed in it当 Combobox 的 BindingSource 属性在其中更改时,C# Windows 窗体 ComboBox 的 SelectionChangeCommitted 事件会阻止选择更改
【发布时间】:2012-10-31 04:29:16
【问题描述】:

使用 C# 实体框架对象,如下 2

项目:

  • 项目名称
  • itemtypeid
  • 商品价格
  • 项目大小

项目类型:

  • 类型标识
  • 类型名称
  • 当前价格
  • 字体大小

在项目编辑表单上有一个名为 typeidComboBox 的组合框绑定到 item.itemtypeid 和从 itemtype 数据源加载的项目列表数据源。

当表单加载时,绑定源将设置为。

    private void Form1_Load(object sender, EventArgs e)
    {
        db = new dbtestEntities();
        itemtypeBindingSource.DataSource = db.usertypes;
        itemBindingSource.DataSource = db.users;

        typeidComboBox.DataBindings.Clear();
        typeidComboBox.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.itemBindingSource, "itemtypeid", true));
        typeidComboBox.DataSource = this.itemtypeBindingSource;
        typeidComboBox.DisplayMember = "typename";
        typeidComboBox.ValueMember = "typeid";
        typeidComboBox.SelectionChangeCommitted += typeidComboBox_SelectionChangeCommitted;
    }

当我在 SelectionChangeCommitted 事件中添加如下代码时出现问题。

代码:

private void typeidComboBox_SelectionChangeCommitted(object sender, EventArgs e)
    {
        (itemBindingSource.Current as item).itemprice = (itemtypeBindingSource.Current as itemtype).currentprice;
    }

当SelectionChangeCommitted 事件的处理方式如Combobox 的BindingSource 属性更改时,为什么Combobox 选择取消并返回旧值?

对不起我的英语。

【问题讨论】:

    标签: winforms entity-framework entity-framework-4 combobox selecteditemchanged


    【解决方案1】:

    我不知道为什么。但它解决了我的问题:DataBinding.WriteValue 和 ComboBox.SelectedItem。

    这是我的工作代码。

    private void typeidComboBox_SelectionChangeCommitted(object sender, EventArgs e)
    {
        foreach (Binding binding in (sender as ComboBox).DataBindings)
            {
                binding.WriteValue();
            }
        (itemBindingSource.Current as item).itemprice = ((sender as ComboBox).SelectedItem as itemtype).currentprice;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-18
      • 2013-03-03
      • 2019-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多