Cannot execute key value in list in C #

I am writing a C # application using winforms. I have a list. I get data from xml file, username and their identifiers. I want the names to appear in the list, and when I select one of them, I want to get his / her identifier using the selectedValue property. However, I cannot do this. I tried keyValuePair, which shows "[username, id]" in a list that is not good (see code below). How can I simulate html select in C # in short? I want the names to appear in the list, but want to get the identifier in the backend. Thanks...

LB_UserList.Items.Add(new KeyValuePair<string, string>(full_name, node["user_id"].InnerText)); 
+6
source share
1 answer

use a dictionary for this,

 Dictionary<string, string> list = new Dictionary<string, string>(); list.Add("item 1", "Item 1"); list.Add("item 2", "Item 2"); list.Add("item 3", "Item 3"); list.Add("item 4", "Item 4"); dropdown.DataSource = list; dropdown.DataTextField = "Value"; dropdown.DataValueField = "Key"; dropdown.DataBind(); 

EDIT:

 listBox.DataSource = new BindingSource(list, null); listBox.DisplayMember = "Value"; listBox.ValueMember = "Key"; 
+14
source

Source: https://habr.com/ru/post/904477/


All Articles