1

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.

Community
  • 1
  • 1
Uriel
  • 13
  • 5
  • You need to post more info, e.g: class denfinition of of items assigned to DataSource property of the combo box. – Mehrzad Chehraz May 12 '15 at 09:58
  • Please give some more information and mention clearly what you want to do. Whats the relation between the code given by you and the article you mentioned? – SajuPK May 12 '15 at 10:02
  • It is probably better to separate both functionalities keep the reading of values from the reader in one function and the population of the list box in another function. that way you will be able to determine where things go wrong – nayef harb May 12 '15 at 10:06
  • Ok since I don't actually know if and how I can edit my question I'll just post here. The reader brings back a value but I want to show the Item assigned for this value. – Uriel May 12 '15 at 10:13
  • I used the exact code from the article I mentioned to populate my combobox with items and hidden values. – Uriel May 12 '15 at 10:21
  • I have 2 items "male", "female" with 1 and 0 values. The reader brings back for ex a value 0 and thus I want it to show the "female" – Uriel May 12 '15 at 10:27

3 Answers3

1

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.

Mehrzad Chehraz
  • 5,092
  • 2
  • 17
  • 28
  • I tried this and it changed my items list to 0 and 1 instead of male and female I had before, plus the combobox had still nothing selected. – Uriel May 12 '15 at 10:37
  • Are you sure that you didn't set "DisplayMember" instead of "ValueMember"? Click "edit" under your question and post your item class and DataSource assignment code. – Mehrzad Chehraz May 12 '15 at 10:40
  • I did exactly as you typed. My item class is the exact same as of the article. I don't have a datasource, I just add items the way I show on my edited question. – Uriel May 12 '15 at 10:50
  • WoW! You are a true Guru my friend. This worked like a charm. I would like to ask you if you can do a bit of analysis on what the "public override bool Equals" is and how did this work but I'm sure won't have the time. – Uriel May 12 '15 at 11:20
  • I added little bit more info to the answer. Fore more info, see Equals in MSDN and/or check here https://msdn.microsoft.com/en-us/library/dd183752.aspx – Mehrzad Chehraz May 12 '15 at 11:35
0

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.

Mehrzad Chehraz
  • 5,092
  • 2
  • 17
  • 28
niklasda
  • 116
  • 6
  • Yeah, thats what fails. The reader brings back a value stored in my db but I want the combobox to show the item assigned to this value. "if you populated it with instances of class x you need to set selectedItem to an instance of class x" That's what I don't know how to do. :/ – Uriel May 12 '15 at 10:16
  • create a new instance of whatever clss you had and initialize it with values from database if possible. Or look throught the items in your combobox and find the one you want to select, the set SelectedIndex to that items index if that a possibility for you. – niklasda May 12 '15 at 10:24
  • I have 2 items "male", "female" with 1 and 0 values respectively. The reader brings back for ex a value 0 and thus I want it to show the item "female" on my combobox – Uriel May 12 '15 at 10:26
  • The it might be a simple trick to use SelectedIndex instead of SelectedItem – niklasda May 12 '15 at 10:28
  • This could be a workaround but what happens when I use Id's instead? – Uriel May 12 '15 at 10:34
  • The map you Id to an Index, in the simple case of male/female it should be simple: genderbox.SelectedIndex = reader["c_gender"].ToString()=="male" ? 1 : 0; – niklasda May 12 '15 at 10:40
0

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;
Cadburry
  • 1,844
  • 10
  • 21