ComboBox does not update DataBindings for the selected item (WinForms)

I have a ComboBox associated with a data source, but it will not update the bindings until the control loses focus. How can I get bindings to update when changing selected items? In the screenshot below, I want the label to be updated immediately to display the new selection.

Code:

public enum MyEnum { First, Second } public class MyData { public String Name { get; set; } public MyEnum MyEnum { get; set; } } 

Form Example:

 public SampleForm() { InitializeComponent (); MyData data = new MyData () { Name = "Single Item" }; this.bindingSource1.DataSource = data; this.comboBox1.DataSource = Enum.GetValues (typeof (MyEnum)); this.label2.DataBindings.Add ("Text", this.bindingSource1, "MyEnum", true, DataSourceUpdateMode.OnPropertyChanged); this.comboBox1.DataBindings.Add (new System.Windows.Forms.Binding ("SelectedItem", this.bindingSource1, "MyEnum", true)); this.comboBox1.DataBindings.Add (new System.Windows.Forms.Binding ("SelectedValue", this.bindingSource1, "MyEnum", true)); } 
+6
source share
2 answers

Comment on the version of SelectedItem and change the binding of SelectedValue, for example, to enable UpdateMode:

 this.comboBox1.DataBindings.Add(new Binding( "SelectedValue", this.bindingSource1, "MyEnum", true, DataSourceUpdateMode.OnPropertyChanged)); 
+6
source

LarsTech solution is right. You can also do this in development mode:

  • ComboBox (F4) Properties -> DataBindings node -> Advanced

  1. Click "SelectedValue" and change the data source update mode to "OnPropertyChanged", enter image description here
0
source

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


All Articles