在C#中不能做的键值在列表框中 [英] Cannot do key-value in listbox in C#

查看:119
本文介绍了在C#中不能做的键值在列表框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用winforms编写一个C#应用程序。我有一个列表框。我从xml文件,用户名和他们的id获取我的数据。我想要的名称显示在列表框中,当我选择其中之一,我想获得他/她的id使用selectedValue属性。但我不能这样做。我试过keyValuePair,它在列表框中显示[username,id],这是不好的(见下面的代码)。如何在c#中模拟html select?我想要的名称显示在列表框中,但想获得id在后端。感谢...

i'm writing a C# app using winforms. I have a listbox. I get my data from xml file, user name and their id's. I want names to be shown in the listbox and when I select one of them, I want to get his/her id using selectedValue property. However I cannot do this. I tried keyValuePair which shows "[username, id]" in the listbox which is not good (see code below). How can I simulate html select in c# in short? I want names to be shown in the listbox but want to get id's in the backend. Thanks...

LB_UserList.Items.Add(new KeyValuePair<string, string>(full_name, node["user_id"].InnerText));


推荐答案

use c#词典

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();






em>

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

这篇关于在C#中不能做的键值在列表框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆