2017-03-16 80 views
0

我在Windows窗体应用程序中创建Combobox我希望在第0个索引值“选择”我该怎么做?我试过在Windows窗体Combobox默认值

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

是否要选择第一项的值或将默认选择的组合框设置为第一项? – ReadyFreddy

+0

先生我想设置默认选择 –

回答

2

你可以这样做:在第0指数

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

插入,然后将其设置为selectedIndex

+1

谢谢先生它正常工作 –

+0

先生它的工作当combobox是空的,但如果combox绑定与数据源然后它不工作,因为我想添加“选择”在第0索引而combobox与数据绑定来源 –

+0

先生它显示以下错误({“设置数据源属性时不能修改项目集合。”)) –

0
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"; 
}