C# 如何在列表框中显示词典
嘿,我在列表框中显示我的字典有点问题C# 如何在列表框中显示词典,c#,asp.net,.net-3.5,C#,Asp.net,.net 3.5,嘿,我在列表框中显示我的字典有点问题 public static Dictionary<String, List<String>> MyDict = new Dictionary<string, List<String>>(); ... if(MyDict[value1] == null){ List<String> Temp_List = new List<String>();
public static Dictionary<String, List<String>> MyDict = new Dictionary<string,
List<String>>();
...
if(MyDict[value1] == null){
List<String> Temp_List = new List<String>();
Temp_List.Items.Add(sth);
MyDict[value1] = Temp_List;
}
else if(MyDict[value1].Count < 4){
...
List<String> Temp_List = new List<String>();
Temp_List = MyDict[value1];
Temp_List.Add(sth);
MyDict[value1] = Temp_List;
}
我不能使用BindingSource。。。有什么想法吗?词典绑定示例:-
字典myDictionary=新字典
添加(1,“测试”);
添加(2,“test2”);
值=1
要显示的文本=“测试”
等等
下拉绑定:-
list1.DataSource = myDictionary;
list1.DataValueField = "Key";
list1.DataTextField = "Value";
list1.DataBind();
list1.DataSource=myDictionary;
list1.DataValueField=“Key”;
list1.DataTextField=“Value”
list1.DataBind();
希望这有帮助,为什么不使用ListView呢
private void PopulateListView(Dictionary<string, string> items, ListView lv)
{
lv.Items.Clear();
foreach (KeyValuePair<string, string> kvp in items)
{
ListViewItem lvi = new ListViewItem(kvp.Value);
lvi.SubItems.Add(kvp.Key);
lv.Items.Add(lvi);
}
}
private void PopulateListView(字典项,ListView lv)
{
lv.Items.Clear();
foreach(项中的KeyValuePair kvp)
{
ListViewItem lvi=新ListViewItem(kvp.Value);
lvi.SubItems.Add(kvp.Key);
lv.项目。添加(lvi);
}
}
ListView的好处(根据我最近的经验)是可以支持两个以上的列
list1.DataSource = myDictionary;
list1.DataValueField = "Key";
list1.DataTextField = "Value";
list1.DataBind();
private void PopulateListView(Dictionary<string, string> items, ListView lv)
{
lv.Items.Clear();
foreach (KeyValuePair<string, string> kvp in items)
{
ListViewItem lvi = new ListViewItem(kvp.Value);
lvi.SubItems.Add(kvp.Key);
lv.Items.Add(lvi);
}
}