Combobox de enlace Usando el Diccionario como Fuente de datos


Estoy usando.NET 2.0 y estoy tratando de vincular la fuente de datos de un combobox a un diccionario ordenado.

Así que el error que estoy obteniendo es "DataMember property 'Key' cannot be found on the 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";
Author: user803952, 2011-06-20

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";

Pero ¿por qué está configurando el ValueMember a "Valor", no debería estar vinculado a "Clave" (y DisplayMember a "Valor" también)?

 112
Author: Sorin Comanescu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-06-20 14:35:25

Utilicé la solución de Sorin Comanescu, pero encontré un problema al intentar obtener el valor seleccionado. Mi combobox era un toolstrip combobox. Usé la propiedad "combobox", que expone un combobox normal.

Tenía un

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

Código vinculante (solución de Sorin Comanescu - funcionó como un encanto):

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

El problema fue que cuando traté de obtener el valor seleccionado, no me di cuenta de cómo recuperarlo. Después de varios intentos conseguí esto:

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

Espero que ayude a alguien ¡else!

 19
Author: CristisS,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-06-06 06:38:25
        var colors = new Dictionary < string, string > ();
        colors["10"] = "Red";

Enlace a Combobox

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

Fuente completa...Diccionario como fuente de datos Combobox

Jeryy

 6
Author: jeryymanly,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-10-08 14:28:14
userListComboBox.DataSource = userCache.ToList();
userListComboBox.DisplayMember = "Key";
 3
Author: smashrain,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-03-20 12:19:11

Un diccionario no se puede usar directamente como fuente de datos, debería hacer más.

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";
 2
Author: DeveloperX,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-08-02 08:51:18

Si esto no funciona, ¿por qué no simplemente hacer un bucle foreach sobre el diccionario agregando todos los elementos a la caja combinada?

foreach(var item in userCache)
{
    userListComboBox.Items.Add(new ListItem(item.Key, item.Value));
}
 0
Author: thekip,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-06-20 14:28:54

Use {>

comboBox1.DataSource = colors.ToList();

A menos que el diccionario se convierta en lista, el combo-box no puede reconocer a sus miembros.

 0
Author: Swaraj,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-01-07 06:03:49

Solo trata de hacer esto....

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";
    }
 0
Author: i486you,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-02-25 10:14:21