1

I'm trying to add objects into a combobox and use SelectedValue property to select and item in the combobox but it does not work: SelectedValue is still null after the assignment.

 class ComboBoxItem
 {
 string name;
 object value;
 public string Name { get { return name; } }
 public object Value { get { return value; } }
 public ComboBoxItem(string name, object value)
 {
 this.name = name;
 this.value = value;
 }
 public override bool Equals(object obj)
 {
 ComboBoxItem item = obj as ComboBoxItem;
 return item!=null && Value.Equals(item.Value);
 }
 } 
 operatorComboBox.Items.Add(new ComboBoxItem("Gleich", SearchOperator.OpEquals));
 operatorComboBox.Items.Add(new ComboBoxItem("Ungleich", SearchOperator.OpNotEquals));
 operatorComboBox.ValueMember="Value";
 //SelectedValue is still null after this statement
 operatorComboBox.SelectedValue = SearchOperator.OpNotEquals; 
Mark Rotteveel
110k241 gold badges160 silver badges233 bronze badges
asked Jun 29, 2010 at 13:10

1 Answer 1

5

ValueMember is only applicable when databinding via DataSource property, not when you add items manually with Items.Add. Try this:

var items = new List<ComboBoxItem>();
items.Add(new ComboBoxItem(...));
operatorComboBox.DataSource = items;

Btw, note that when you override Equals, you should also override and implement GetHashCode.

answered Jun 29, 2010 at 13:18
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.