I am having some trouble figuring out the cause of a small problem with one of my combo boxes.
I am trying to bind cboDropDownList.Text to the value of an object's property. The object implements INotifyPropertyChanged and I have added the DataBinding to the combobox programmatically. Here's what the code looks like for the object and the combo box:
Object:
public partial class ObjectType : object, INotifyPropertyChanged{
private string objectIdField;
public string objectId {
get
{
return this.objectIdField;
}
set
{
this.objectIdField = value;
this.RaisePropertyChanged("objectId");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
if (propertyChanged != null)
{
propertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Combo Box:
this.cboDropDownList.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.ObjectBindingSource, "objectId", true));
this.cboDropDownList.DataSource = this.DataSetBindingSource;
this.cboDropDownList.DisplayMember = "item_id";
this.cboDropDownList.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cboDropDownList.FormattingEnabled = true;
this.cboDropDownList.Location = new System.Drawing.Point(177, 109);
this.cboDropDownList.Name = "cboDropDownList";
this.cboDropDownList.Size = new System.Drawing.Size(155, 21);
this.cboDropDownList.TabIndex = 27;
this.cboDropDownList.ValueMember = "item_id";
The combo box's content (the selectable items) is successfully bound to items from a DataSet. However, when the combo box is loaded, it always displays the same thing. For example, if the value of objectId
is 15, cboDropDownList
will display 1, and it will display 1 no matter the value of objectId
. (1 is the first item in the DropDownList)
Would anyone have any ideas as to why this is happening?
Thanks!