c# - I'm retrieving a value for a combobox with custom class items but I can't make it show the item -


i wanted have items , hidden values call later used article create custom items.

but i'm calling 1 value cannot make show proper item. combobox stays null.

if (reader.hasrows) {     reader.read();     namebox.text = reader["c_name"].tostring();     lastbox.text = reader["c_lastname"].tostring();     genderbox.selecteditem = reader["c_gender"].tostring(); } 

here add combobox , want show accoring value reader

 private void editcust_load(object sender, eventargs e)         {             genderbox.items.add(new comboboxitem("male", "1"));             genderbox.items.add(new comboboxitem("female", "0"));         } 

please let me know if need add more code or provide more information. i'm junior developer please excuse terrible mistakes , bad formulation.

first, override equals , gethashcode methods in class:

public class comboboxitem() {      string displayvalue;      string hiddenvalue;       //constructor      public comboboxitem (string d, string h)      {           displayvalue = d;           hiddenvalue = h;      }       //accessor      public string hiddenvalue      {                     {                return hiddenvalue;           }      }       public override bool equals(object obj)      {          comboboxitem item = obj comboboxitem;          if (item == null)          {             return false;          }          return item.hiddenvalue == this.hiddenvalue;      }      public override int gethashcode()      {          if (this.hiddenvalue == null)          {              return 0;          }          return this.hiddenvalue.gethashcode();      }      //override tostring method      public override string tostring()      {           return displayvalue;      }   } 

then assign new item selecteditem property:

genderbox.selecteditem = new comboboxitem(string.empty, reader["c_gender"].tostring()); 

when assign value selecteditem property of combobox, looks in it's items collection , tries find item equal assigned value. if find item equal value, item gets selected. in process, comparison done equals method of each item.

by overriding method, tell combobox compare items using "hiddenvalue" field, when assign new item ite's hiddenvalue set, combobox can find in it's items collection. if don't that, equality comparison done using object references instead.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -