C # Windows Forms ComboBox SelectionChangeCommitted event prevents selection changes when the Combobox BindingSource property has changed in it

Using C # Entity Framework objects like below 2

paragraph:

  • ItemName
  • itemtypeid
  • itemprice
  • itemsize

ItemType:

  • Typeid
  • Type Name
  • currentprice
  • typesize

In the item editing form, there is a combobox called typeidComboBox associated with loading the item.itemtypeid data source and item list from the itemtype data source.

When the Loads Binding Sources forms will be set as.

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; } 

The problem occurs when I add some code, as shown below in the SelectionChangeCommitted event.

code:

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

Why does Combobox cancel the selection and maintain the old value when the SelectionChangeCommitted event is processed as the Combobox BindingSource property changed in it?

Sorry, my English.

+4
source share
1 answer

I do not know why. But he solved my problem: DataBinding.WriteValue and ComboBox.SelectedItem.

Here is my working code.

 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; } 
+2
source

Source: https://habr.com/ru/post/1445336/


All Articles