0

So I have a combo box called cboGender, I want to add values (hard code the values) to the combo box like Male, Female and Unspecified. How do I go about doing it? Thanks in advance.

Nicz
  • 49
  • 2
  • 9
  • 3
    A little bit of research would give you the answer to your question. [Read This](http://stackoverflow.com/questions/14143287/add-items-to-the-combobox). – P. Pat Mar 15 '17 at 02:07

3 Answers3

1

You can add the items by clicking the combo box's item collection editor and enter the items line by line.

Items Collection editor

SUTHANSEE
  • 332
  • 4
  • 13
0

cboGender.Items.Add(new Item("Male"));

Dakota Kincer
  • 448
  • 3
  • 16
0

There could be multiple ways of how you can populate comboBox, you can either add items one by one or you can add a whole collection etc... Adding items one by one could be done like this:

comboBox1.Items.Add("Male");
comboBox1.Items.Add("Female");
comboBox1.Items.Add("Unspecified");

Adding the same above items in a single statement could be done like this:

comboBox1.Items.AddRange(new object[]{ "Male","Female","Unspecified"});

You can set a List of class objects as a datasource to your comboBox as well. Create a class like this:

class personGender
{
  public string gender { get; set; }
}

Set the datasource of comboBox like this:

List<personGender> list = new List<personGender>()
{
 new personGender{gender="Male"},
 new personGender{gender="Female"},
 new personGender{gender="Unspecified"},
};
comboBox1.DataSource = list;
comboBox1.DisplayMember = "gender";

Or you can do the same above this within 2 lines like this:

comboBox1.DataSource = new List<personGender>()
{
 new personGender{gender="Male"},
 new personGender{gender="Female"},
 new personGender{gender="Unspecified"},
};
comboBox1.DisplayMember = "gender";

You can also set its datasource after fetching the records from your database.

Hope it helps!

M. Adeel Khalid
  • 1,786
  • 2
  • 21
  • 24