2017-02-10 25 views
0

我有幾個不同的字典結構,我想要顯示在組合框中。根據組合框中的鍵顯示字典成員

在JumpType.cs:

public SortedDictionary<int, List<string>> jumpCombination = new SortedDictionary<int, List<string>>(); 

字典結構將是這個樣子:

Key Values 
1  Flygande 
     EjFlygande 
2  Bak 
     Pik 
     Test 
3  ... 

我已經創建了兩個組合框在我的UI是這樣的:

Select Key:  _____________ 
       | ComboBox | 
       --------------  __________ 
       _____________  | OK | 
Select Value: | ComboBox |  ---------- 
       -------------- 

在Form1.cs中

InitializeComponent(); 
JumpType jt = new JumpType(); 
jt.addjumpCombination(); // populating the dictionary 
if (jt.jumpCombination != null) 
{ 
    comboBoxJumpComboKey.DataSource = new BindingSource(jt.jumpCombination, null); // Key => null 
    comboBoxJumpComboKey.DisplayMember = "Value"; 
    comboBoxJumpComboKey.ValueMember = "Key"; 
    comboBoxJumpComboValue.DisplayMember = "Value"; 
} 

我怎麼會去根據選擇鍵選擇相應的值?

在此先感謝。

+0

列表值= jumpCombination [關鍵] – jdweng

回答

0

您可以使用下面的LINQ查詢過濾器列表:

var selectedValues = jumpCombination 
        .Where(j => j.Key == Convert.ToInt32(comboBoxJumpComboKey.SelectedItem.Value)) 
        .Select(a => a.Value) 
        .ToList(); 

此外,selectedValues是列表是你的情況

+0

的集合不幸的是無法工作。 SelectedItem.Value無效語法:/ – Joel

+0

奇怪! comboBoxJumpComboKey​​不是組合框嗎?那麼comboBoxJumpComboKey​​.SelectedValue呢?那對你有用嗎? – RRM

相關問題