4

以下の 2 のような C# Entity Framework オブジェクトの使用

アイテム:

  • 項目名
  • itemtypeid
  • アイテムの価格
  • アイテムサイズ

アイテムタイプ:

  • typeid
  • タイプ名
  • 現在の価格
  • タイプサイズ

アイテム編集フォームには、item.itemtypeid にバインドされた typeidComboBox と呼ばれるコンボボックスと、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 イベントがコンボボックスの BindingSource プロパティが変更されたように処理されたときに、コンボボックスの選択がキャンセルされて古い値に戻るのはなぜですか?

申し訳ありませんが私の英語。

4

1 に答える 1

2

どうしてか分かりません。しかし、それは私の問題を解決しました: 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;
}
于 2012-11-19T08:15:49.537 に答える