Michael
Michael

Reputation: 13636

Add Items to the ComboBox

I have a ComboBox control.

I bind to this control to the DataSet table.

Here is the code:

comboBox.Items.Add(("Select"));
comboBox.DataSource = DataSet.ColorTable;
comboBox.DisplayMember = DataSet.ColorTable.ColorNameColumn.ColumnName;
comboBox.ValueMember = DataSet.ColorTable.ColorIDColumn.ColumnName;

This result I get:

enter image description here

I want to display on the top of the list SELECT: word. So I need to add addition Item to the comboBox control. Here how I implement it:

cmbCategory.Items.Add(("Select"));

But the result is still the same as above. I get only colors without SELECT: word on the top of the list.

Any idea how I can add this string-SELECT: to the ComboBox control and set to this string ValueMember?

Upvotes: 4

Views: 56366

Answers (5)

Bhaskar
Bhaskar

Reputation: 1036

You can add the collections of color to an array or a dataset (if you are getting them from database) first and then add items "select", then add each elements of the array or a column of the dataset.

Do this in Form_Load function and wherever there are changes made in color collections array or database.

Upvotes: 3

YiagosEE
YiagosEE

Reputation: 1

If we want to add manually values in a combobox (for example integers) this can be done using a for loop:

// sample code
int lower=1;
int higher=500;

for (int i=lower; i<=higher; i++)
 combo_values.Items.Add(i.ToString());

Note that you have to use the int.Parse(combo_values.Text) command to read a value.

Upvotes: 0

Thilina Sandunsiri
Thilina Sandunsiri

Reputation: 590

            //This will set Display member and value member
            comboBox.DisplayMember = "ColorName";
            comboBox.ValueMember = "ColorCode";

          //This will add a new row to table in binded dataset
            DataRow dr = dal.MyProperty_dsColors.Tables["ColorInfo"].NewRow();
            dr["ColorName"] = "Select Color"; //SomeName
            dr["ColorCode"] = 001; //Some ID
            dal.MyProperty_dsColors.Tables["ColorInfo].Rows.Add(dr); 

           //binding dataSource
            comboBox.DataSource = dal.MyProperty_dsColors.Tables["ColorInfo"];

Upvotes: 2

Johnaudi
Johnaudi

Reputation: 257

What would also help you is that you set the ComboBox without having to 'Select' it when the popup arrives... Select your ComboBox, under the properties tab, select Appearance->Drop Down Style and select DropDownList.

Upvotes: 1

Kundan Singh Chouhan
Kundan Singh Chouhan

Reputation: 14302

Use Insert method instead.

cmbCategory.Items.Insert(0, "Select");

Note : Put this code after the databind.

Upvotes: 8

Related Questions