Trade.TradeStatus is an instance of the class TradeOrderStatus
You mention it is a class; is it the same instance as one of those in the data-bound list? It needs to find an equality match. Alternatively, you can override Equals
(and GetHashCode()
- always keep the two in sync) to achieve the same thing.
(edit)
The simplest way to fix it is to bind to SelectedValue
; with the "full" example (below), something like:
cbo.DisplayMember = cbo.ValueMember = "Name";
...
btn.Click += delegate { cbo.SelectedValue = GetCurrentStatus().Name; };
(edit)
Here's a C# example (sorry, my VB-fu is weak) of providing custom equality of the different statuses - note the "uncomment to fix":
using System.Windows.Forms;
using System.Collections.Generic;
class MyStatus {
public MyStatus(string name) { Name = name; }
public string Name { get; private set; }
public override string ToString() {return Name; }
/* uncomment to fix
public override bool Equals(object obj) {
MyStatus other = obj as MyStatus;
return other != null && other.Name == this.Name;
}
public override int GetHashCode() {return Name.GetHashCode(); }
*/
}
static class Program {
static void Main() {
ComboBox cbo = new ComboBox();
cbo.DataSource = GetStatuses();
Button btn = new Button();
btn.Click += delegate { cbo.SelectedItem = GetCurrentStatus(); };
btn.Text = "Set Status";
btn.Dock = DockStyle.Bottom;
Application.Run(new Form { Controls = { cbo, btn } });
}
static List<MyStatus> GetStatuses() {
List<MyStatus> stats = new List<MyStatus>();
stats.Add(new MyStatus("Open"));
stats.Add(new MyStatus("Pending"));
stats.Add(new MyStatus("Closed"));
return stats;
}
static MyStatus GetCurrentStatus() {
return new MyStatus("Closed");
}
}