我正在尝试将键/值对从字典显示到列表框。
Key Value
A 10
B 20
C 30
我想以下列格式在列表框中显示它们
A(10)
B(20)
C(30)
使用以下代码,我已经能够将 Listbox.Datasource 链接到 Dictionary。
myListBox.DataSource = new BindingSource(myDictionary, null);
它显示为
[A, 10]
[B, 20]
[C, 30]
我不知道如何格式化它以便它以我想要的方式显示。
我们将不胜感激。
谢谢 阿希什
最佳答案
在列表框上使用 Format 事件:
KeyValuePair<string, int> item = (KeyValuePair<string, int>)e.ListItem;
e.Value = string.Format("{0}({1})", item.Key, item.Value);
关于dictionary - 如何在列表框中显示字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/885271/