2011-06-20 112 views
54

我使用.NET 2.0,並試圖將組合框的數據源綁定到已排序的字典。使用字典作爲數據源的綁定組合框

所以我得到的錯誤是「DataMember屬性」鍵「無法在數據源中找到」。

 SortedDictionary<string, int> userCache = UserCache.getSortedUserValueCache(); 
     userListComboBox.DataSource = new BindingSource(userCache, "Key"); //This line is causing the error 
     userListComboBox.DisplayMember = "Key"; 
     userListComboBox.ValueMember = "Value"; 

回答

105
SortedDictionary<string, int> userCache = new SortedDictionary<string, int> 
{ 
    {"a", 1}, 
    {"b", 2}, 
    {"c", 3} 
}; 
comboBox1.DataSource = new BindingSource(userCache, null); 
comboBox1.DisplayMember = "Key"; 
comboBox1.ValueMember = "Value"; 

但是你爲什麼設置ValueMember到「值」,應該不是被綁定到「密鑰」(和DisplayMember以「價值」爲好)?

+0

那麼它不應該無論如何。但切換兩者可能更有意義。但是我遇到的問題是「comboBox1.DataSource = new BindingSource(userCache,null);」我不能在那裏拉空,因爲它給我一個錯誤。 – user803952

+1

什麼錯誤,這實際上適用於我? –

+1

「ArgumentException:無法綁定到新的顯示成員。參數名稱:newDisplayMember。」我不知道user803952得到了什麼錯誤,但是這是我用'IDictionary '嘗試這樣做時發生的錯誤。 –

0

如果這不起作用,爲什麼不能簡單地做了字典將所有的項目組合框foreach循環?

foreach(var item in userCache) 
{ 
    userListComboBox.Items.Add(new ListItem(item.Key, item.Value)); 
} 
+0

綁定和添加的項目是不一樣的東西。那麼,也許這就是OP真正需要的,誰知道呢? ;) – jv42

+0

我知道,但我沒有看到任何依賴於數據綁定本身的代碼。 – thekip

+0

那麼你建議可以工作,但「System.Web.UI.WebControls命名空間中存在」新的ListItem「,我不會爲Windows窗體應用程序導入。 – user803952

2

字典不能直接用作數據源,你應該做更多。

SortedDictionary<string, int> userCache = UserCache.getSortedUserValueCache(); 
KeyValuePair<string, int> [] ar= new KeyValuePair<string,int>[userCache.Count]; 
userCache.CopyTo(ar, 0); 
comboBox1.DataSource = ar; new BindingSource(ar, "Key"); //This line is causing the error 
comboBox1.DisplayMember = "Value"; 
comboBox1.ValueMember = "Key"; 
18

我使用了Sorin Comanescu的解決方案,但在嘗試獲取選定值時遇到了問題。我的組合框是一個工具欄組合框。我使用了「combobox」屬性,它暴露了一個正常的組合框。

我有一個

Dictionary<Control, string> controls = new Dictionary<Control, string>(); 

綁定代碼(索林Comanescu的解決方案 - 工作就像一個魅力):

controls.Add(pictureBox1, "Image"); 
controls.Add(dgvText, "Text"); 
cbFocusedControl.ComboBox.DataSource = new BindingSource(controls, null); 
cbFocusedControl.ComboBox.ValueMember = "Key"; 
cbFocusedControl.ComboBox.DisplayMember = "Value"; 

的問題是,當我試圖讓所選擇的價值,我沒有」不知道如何檢索它。經過幾次嘗試,我得到了這個:

var control = ((KeyValuePair<Control, string>) cbFocusedControl.ComboBox.SelectedItem).Key 

希望它可以幫助別人!

+0

這可以工作,我已經在我自己的代碼中使用了以下內容來使其工作。 https://gist.github.com/psykzz/5374823 – PsyKzz

+0

你也可以這樣做(獲取選定的值): var value = comboBox.SelectedItem; var someItem = value.GetType()。GetProperty(「Key」)。GetValue(value,null); –

+1

最後一行也可以縮小爲:var control =((KeyValuePair )cbFocusedControl.ComboBox.SelectedItem).Key; 1)不必要的外部括號,2)編譯器知道Key是控件,因爲它正在轉換爲KeyValuePair ,因此不需要強制轉換爲Control。 –

3
userListComboBox.DataSource = userCache.ToList(); 
userListComboBox.DisplayMember = "Key"; 
6
 var colors = new Dictionary < string, string >(); 
     colors["10"] = "Red"; 

結合的ComboBox

 comboBox1.DataSource = new BindingSource(colors, null); 
     comboBox1.DisplayMember = "Value"; 
     comboBox1.ValueMember = "Key"; 

全部源... Dictionary as a Combobox Datasource

Jeryy

0

使用 - >

comboBox1.DataSource = colors.ToList(); 

除非字典轉換爲列表,否則組合框無法識別其成員。

0

只是嘗試做這樣的....

SortedDictionary<string, int> userCache = UserCache.getSortedUserValueCache(); 

    // Add this code 
    if(userCache != null) 
    { 
     userListComboBox.DataSource = new BindingSource(userCache, null); // Key => null 
     userListComboBox.DisplayMember = "Key"; 
     userListComboBox.ValueMember = "Value"; 
    } 
相關問題