开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜