0

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!

Gabbie
  • 31
  • 1
  • 9
  • Change this.cboDropDownList.DisplayMember = "Write what yo want to see here"; – Kemal Güler Jul 11 '17 at 14:06
  • Thanks for the suggestion @KemalGüler , unfortunately it doesn't seem to be working in my case – Gabbie Jul 11 '17 at 14:17
  • I believe this is what you're looking for: https://stackoverflow.com/questions/2417960/populating-a-combobox-using-c-sharp – Ryan C Jul 11 '17 at 14:24
  • Thanks @RyanCarlisle, but my DisplayMember/ValueMember are already working just fine, it's just that when the value of "objectID" changes, I want the corresponding item in the combo box to be selected and displayed. – Gabbie Jul 11 '17 at 14:31
  • Have you tried by creating a binding to 'SelectedItem' – JSR Jul 11 '17 at 15:39
  • @JSR I have just now tried to "SelectedItem" instead of "Text" and it didn't work, sadly – Gabbie Jul 11 '17 at 16:28
  • Like JSR says, change the bound property to "SelectedItem", but also override the equals method in your ObjectType class to use objectId to check for equality. I haven't tried it, just a tought :) – TDull Jul 14 '17 at 09:35

0 Answers0