Binding Combobox Using Dictionary as the Datasource
Używam. NET 2.0 i próbuję powiązać źródło danych comboboxu z posortowanym słownikiem.
Więc otrzymuję błąd to "właściwość DataMember' Key 'nie może zostać znaleziona w Datasource".
SortedDictionary<string, int> userCache = UserCache.getSortedUserValueCache();
userListComboBox.DataSource = new BindingSource(userCache, "Key"); //This line is causing the error
userListComboBox.DisplayMember = "Key";
userListComboBox.ValueMember = "Value";
8 answers
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";
Ale dlaczego ustawiasz ValueMember
na "wartość", nie powinno być powiązane z "kluczem" (i DisplayMember
na "wartość", jak również)?
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-06-20 14:35:25
Użyłem rozwiązania Sorina Comanescu, ale napotkałem problem podczas próby uzyskania wybranej wartości. Mój combobox był ToolStrip combobox. Użyłem właściwości "combobox", która eksponuje normalny combobox.
Miałem
Dictionary<Control, string> controls = new Dictionary<Control, string>();
Kod wiążący (rozwiązanie Sorina Comanescu-zadziałało jak urok):
controls.Add(pictureBox1, "Image");
controls.Add(dgvText, "Text");
cbFocusedControl.ComboBox.DataSource = new BindingSource(controls, null);
cbFocusedControl.ComboBox.ValueMember = "Key";
cbFocusedControl.ComboBox.DisplayMember = "Value";
Problem polegał na tym, że gdy próbowałem uzyskać wybraną wartość, nie wiedziałem, jak ją odzyskać. Po kilku próbach mam to:
var control = ((KeyValuePair<Control, string>) cbFocusedControl.ComboBox.SelectedItem).Key
Mam nadzieję, że to komuś pomoże else!
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-06-06 06:38:25
var colors = new Dictionary < string, string > ();
colors["10"] = "Red";
Wiązanie z Comboboxem
comboBox1.DataSource = new BindingSource(colors, null);
comboBox1.DisplayMember = "Value";
comboBox1.ValueMember = "Key";
Pełne Źródło...Słownik jako źródło danych Combobox
Jeryy
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-10-08 14:28:14
userListComboBox.DataSource = userCache.ToList();
userListComboBox.DisplayMember = "Key";
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-03-20 12:19:11
Słownik nie może być bezpośrednio używany jako źródło danych, powinieneś zrobić więcej.
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";
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-08-02 08:51:18
Jeśli to nie zadziała, dlaczego po prostu nie zrobić pętli foreach nad słownikiem, dodając wszystkie elementy do comboboxu?
foreach(var item in userCache)
{
userListComboBox.Items.Add(new ListItem(item.Key, item.Value));
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-06-20 14:28:54
Użyj -- >
comboBox1.DataSource = colors.ToList();
Dopóki słownik nie zostanie przekonwertowany na listę, combo-box nie rozpozna swoich członków.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-01-07 06:03:49
Po prostu spróbuj to zrobić....
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";
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-02-25 10:14:21