我不知道这个代码有什么问题..它说字符串无法转换为对象..类似的东西..
//lvLogs <-- ListView (2 colums)
Hashtable serverLogs = new Hashtable();
serverLogs.Add("a", "aw");
serverLogs.Add("b", "ew");
serverLogs.Add("c", "iw");
foreach (DictionaryEntry h in serverLogs)
{
lvLogs.Items.Add(h.Key).SubItems.Add(h.Value);
}
Run Code Online (Sandbox Code Playgroud)
但是这段代码工作得很好..
Hashtable serverLogs = new Hashtable();
serverLogs.Add("a", "aw");
serverLogs.Add("b", "ew");
serverLogs.Add("c", "iw");
foreach (DictionaryEntry h in serverLogs)
{
//lvLogs.Items.Add(h.Key).SubItems.Add(h.Value);
//lvi.SubItems.Add(h.Value);
lvLogs.Items.Add(h.Key + " - " + h.Value);
}
Run Code Online (Sandbox Code Playgroud)
如何从lvLogs中的列中分离键和值?
Hashtable不是强类型的集合.DictionaryEntry.Key返回一个object,并且你试图将它用作string没有强制转换的东西,这是不允许的.
字符串连接工作的原因是它接受object作为参数(它调用ToString()它).
请尝试使用Dictionary<string, string>.