Set the selecteditem of a combobox based on key,value pair.

后端 未结 4 855
情深已故
情深已故 2021-01-27 17:32

I have a combobox that I populate like this:

this.reqTypeInput.Items.Add(new RequestType(\"Label 1\", \"Value1\"));
this.reqTypeInput.Items.Add(new RequestType(\         


        
4条回答
  •  北海茫月
    2021-01-27 18:02

    It looks like you are trying to find the index as though your ComboBox contains just string values, when it actually contains RequestType objects. Have you tried overriding your Equals operator?

    Check out this SO post, and this one for examples of overriding Equals.

    EDIT: As mentioned in another answer, a good practice is to populate a collection of objects that you want in the ComboBox, then bind that collection to your ComboBox. The first link in my answer here has an example of that.

提交回复
热议问题