如何在列表框中显示词典 [英] How to display a Dictionary in a ListBox

查看:110
本文介绍了如何在列表框中显示词典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试显示从字典到列表框的键/值对.

I'm trying to display Key/Value Pairs from a Dictionary to a ListBox.

Key Value
A    10
B    20
C    30

我想以以下格式在列表框中显示它们

I want to display them in a ListBox in following format

A(10)
B(20)
C(30)

使用以下代码,我已经能够将Listbox.Datasource链接到Dictionary.

Using following code I have been able to link Listbox.Datasource to Dictionary.

myListBox.DataSource = new BindingSource(myDictionary, null);

显示为

[A, 10]
[B, 20]
[C, 30]

我不知道如何格式化它,使其以我想要的方式显示.

I can't figure out how to format it so that it is displayed in the way I want.

任何帮助将不胜感激.

谢谢 灰烬

推荐答案

使用列表框中的Format事件:

Use the Format event on the list box:

KeyValuePair<string, int> item = (KeyValuePair<string, int>)e.ListItem;
e.Value = string.Format("{0}({1})", item.Key, item.Value);

这篇关于如何在列表框中显示词典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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