M Tahir Latif
M Tahir Latif

Reputation: 11

In Windows Form Combobox Default value

I’m creating a Combobox in Windows Form Application I Want value "Select" at 0th index how can I do it? I've tried

 cbxWeigtType Item = new cbxWeigtType();
 Item.Text = "my string";
 this.DropDownList1.SelectedItem = Item;

Upvotes: 0

Views: 18051

Answers (2)

M Tahir Latif
M Tahir Latif

Reputation: 11

private void BindComboBoxItem()
{
    ItemRepository repo = new ItemRepository();
    List<Item> items = repo.GetAll();
    List<KeyValuePair<int, string>> allitems = new List<KeyValuePair<int, string>>();
    KeyValuePair<int, string> first = new KeyValuePair<int, string>(0, "Please Select");
    allitems.Add(first);
    foreach (Item item in items)
    {
        KeyValuePair<int, string> obj = new KeyValuePair<int, string>(item.Id, item.Name);
        allitems.Add(obj);
    }
    cbxSelectItem.DataSource = allitems;
    cbxSelectItem.DisplayMember = "Value";
    cbxSelectItem.ValueMember = "Key";
}

Upvotes: 0

M. Adeel Khalid
M. Adeel Khalid

Reputation: 1796

You can do this:

comboBox1.Items.Insert(0, "Select");
comboBox1.SelectedIndex = 0;

Insert at 0th index and then set it as selectedIndex.

Upvotes: 5

Related Questions