开发者

How to display listbox with items such as item.name is shown in the list while the value passed is item.id?

开发者 https://www.devze.com 2023-03-11 11:55 出处:网络
Upon selecting any item from the list, I want some other property to be passed than the one alr开发者_运维问答eady being displayed.First of all, you have to fill your LitBox items. You have two option

Upon selecting any item from the list, I want some other property to be passed than the one alr开发者_运维问答eady being displayed.


First of all, you have to fill your LitBox items. You have two options here:

1 - By adding items

listBox.Items.Add(new KeyValuePair<Object, String>("Key", "Text"));

2 - By binding to a data source

listBox.DataSource = objectDataSource;
listBox.DisplayMember = "Name";
listBox.ValueMember = "ID";

Now, to get the selected item you can do the following:

KeyValuePair<Object, String> listBoxItem = 
    (KeyValuePair<Object, String>listBox.SelectedItem;
String text = listBoxItem.Value.ToString();
Object key = listBoxItem.Key;

Refer to :

ListBox Class

KeyValuePair Structure


Set listbox DataSource to your data source

Set listbox ValueMember to the "Id"

Set listbox DisplayMember to the value you want displayed, i.e. "Name".

0

精彩评论

暂无评论...
验证码 换一张
取 消