我在Windows窗体应用程序中创建Combobox
我希望在第0个索引处选择“选择”我该怎么办?我试过了
cbxWeigtType Item = new cbxWeigtType();
Item.Text = "my string";
this.DropDownList1.SelectedItem = Item;
答案 0 :(得分:2)
你可以这样做:
comboBox1.Items.Insert(0, "Select");
comboBox1.SelectedIndex = 0;
在第0个索引处插入,然后将其设置为selectedIndex
。
答案 1 :(得分: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";
}