ComboBox not updating DataBindings on selected item changed (WinForms)
19,293
Solution 1
Comment out the SelectedItem version, and modify the SelectedValue binding like this to include the UpdateMode:
this.comboBox1.DataBindings.Add(new Binding(
"SelectedValue",
this.bindingSource1,
"MyEnum",
true,
DataSourceUpdateMode.OnPropertyChanged));
Solution 2
LarsTech solution is correct. You can also do it in design mode:
Author by
kbeal2k
Updated on June 11, 2022Comments
-
kbeal2k almost 2 years
I have a ComboBox bound to a data source but it will not update the bindings until the control loses focus. How can I get the bindings to update when the selected items change? In the screen shot below I'd like the label to updated immediately to reflect the new selection.
Some Code:
public enum MyEnum { First, Second } public class MyData { public String Name { get; set; } public MyEnum MyEnum { get; set; } }
Sample Form:
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)); }
-
ehmunnehm almost 7 yearsThis worked for me too. Thanks. But it doesn't load the inital value of MyEnum. Do I have to do this manually?
-
LarsTech almost 7 years@ehmunnehm I don't know what your code looks like to answer that. Try posting a new question with the proper reproduction code.