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

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

问题描述

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

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

Key Value
A    10
B    20
C    30

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

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天全站免登陆