Uriel
Uriel

Reputation: 13

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

I wanted to have items and hidden values which I could call later so I used this Article to create my custom items.

But now that I'm calling one value I cannot make it show the proper item. The 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 is what I add to my combobox and what I want to show accoring to what value I get from the 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 I need to add more code or provide more information. I'm a junior developer so please excuse my terrible mistakes and bad formulation.

Upvotes: 1

Views: 1910

Answers (3)

Mehrzad Chehraz
Mehrzad Chehraz

Reputation: 5137

First, override Equals and GetHashCode methods in your class:

public class ComboBoxItem()
{
     string displayValue;
     string hiddenValue;

     //Constructor
     public ComboBoxItem (string d, string h)
     {
          displayValue = d;
          hiddenValue = h;
     }

     //Accessor
     public string HiddenValue
     {
          get
          {
               return hiddenValue;
          }
     }

     public override bool Equals(object obj)
     {
         ComboBoxItem item = obj as 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 a new Item to the SelectedItem property:

genderbox.SelectedItem = new ComboBoxItem(string.Empty, reader["c_gender"].ToString());

When you assign a value to the SelectedItem property of ComboBox, it looks in it's items collection and tries to find an item that is equal to the assigned value. If it find an item equal to the value, that item gets selected. In the process, comparison is done by the Equals method of each item.

By overriding the method, you tell ComboBox to compare items using the "hiddenValue" field, so when you assign a new item with ite's hiddenValue set, combobox can find it in it's items collection. If you don't do that, equality comparison will be done using object references instead.

Upvotes: 1

niklasda
niklasda

Reputation: 116

Agreed the question is unclear but if you mean that this call fails:

genderbox.SelectedItem = reader["c_gender"].ToString();

It's probably because that you need to use the same kind of value that you originally populated the list with.

i.e. if you populated it with instances of class x you need to set selectedItem to an instance of class x.

Upvotes: 0

Cadburry
Cadburry

Reputation: 1864

Use the DisplayMember & ValueMember properties of the ComboBox-Class and assign a DataSource.

ie. Your Data Class:

     private class yourDataClass
    {
        public string DisplayMemberProperty { get; set; }
        public int IDMember { get; set; }
    }

Assign the datasource with values to the combobox

      var dataSource = new ArrayList();

        dataSource.Add(new yourDataClass() { DisplayMemberProperty = "Hello", IDMember = 1 });
        dataSource.Add(new yourDataClass() { DisplayMemberProperty = "Hello2", IDMember = 2 });
        dataSource.Add(new yourDataClass() { DisplayMemberProperty = "Hello3", IDMember = 2 });

        this.comboBox1.DataSource = dataSource;
        this.comboBox1.DisplayMember = "DisplayMemberProperty";
        this.comboBox1.ValueMember = "IDMember";

Retreive the selected value...

        var value = this.comboBox1.SelectedValue;

Upvotes: 0

Related Questions