So basically, I have an entity, I've used a LINQ query to get some items and bound that to a combobox.
What I'm trying to do is have the entity update when the item in the combobox is changed without doing a manual update in the Combobox.SelectedIndex/SelectedValue changed event. I'm just wondering if there is a better way?
Pseudo code:
SomeEntity se = new SomeEntity();
var q = from x in se select x;
cbComboBox.Datasource = q.ToList();
cbComboBox.DisplayMember = "SomeValue";
cbComboBox.ValueMember = "SomeValueID";
cbComboBox.SelectedValue = se.SomeValueID;
So the combobox shows the correct value I want. That's fine.
Here's the thing, when the user changes the combobox, I want se.SomeValueID to reflect that change.
I know I can set se.SomeValueID in the combobox SelectedValue event, but is there a better way? Where se.SomeValueID automagically gets updated?
Assuming your question is about Windows Forms. To bind
SelectedValue
to an object's property you can add custom binding:That binding is two-way and there's no need in
cbComboBox.SelectedValue = se.SomeValueID;
line.