在發佈此問題之前,我認爲這是一個簡單的問題,我搜索答案並沒有找到合適的解決方案。設置ComboBox按鍵不是按值選擇項c#
在我的日常工作,我與Web應用程序的工作,並可以很容易地獲取或設置
我不能做相同的Windows應用程序C#dropdownlists值
我有組合框和階級的ComboItem
public class ComboItem
{
public int Key { get; set; }
public string Value { get; set; }
public ComboItem(int key, string value)
{
Key = key; Value = value;
}
public override string ToString()
{
return Value;
}
}
說該組合框是通過硬編碼綁定和的值是
- 重點:1 /值:男性
鍵:2 /價值:女
鍵:3 /值:未知
可以說,我有鑰匙= 3,我想通過代碼 設置這個項目(其關鍵是3),所以當表單被加載時,默認選擇的值將是未知的。
combobox1.selectedValue =3 //Not Working , selectedValue used to return an object
combobox1.selectedIndex = 2 //Working as 2 is the index of key 3/Unknown
但讓我說我不知道索引,我怎麼得到的項目的索引,其中的關鍵= 3?
指數可在這樣
int index = combobox1.FindString("Unknown") //will return 2
查找字符串取一個值不是關鍵,通過價值的東西,我需要像查找字符串內搭鍵和返回指數
注: 這裏是如何我結合我的下拉菜單
JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings();
jsonSerializerSettings.MissingMemberHandling = MissingMemberHandling.Ignore;
var empResult= await response.Content.ReadAsStringAsync();
List<Emp> Emps= JsonConvert.DeserializeObject<Emp[]>(empResult, jsonSerializerSettings).ToList();
foreach (var item in Emps)
{
ComboItem CI = new ComboItem(int.Parse(item.ID), item.Name);
combobox1.Items.Add(CI);
}
this.combobox1.DisplayMember = "Value";
this.combobox1.ValueMember = "Key";
Windows Forms? WPF?要麼 ? –
Windows窗體C# – Bassem