读书人

dictionary已平添了具有相同键的项

发布时间: 2013-09-12 22:07:00 作者: rapoo

dictionary已添加了具有相同键的项
dictionary已添加了具有相同键的项,这个怎么搞啊dictionary已平添了具有相同键的项
[解决办法]
dictionary是个字典,它的key不允许重复,你肯定是重复添加了相同的key的value,所以报这个错。
[解决办法]

 private DataTable ShowRowData(string path)
{
// string path = txtFileName.Text;

// DataSet ds = new DataSet();

DataRow dr = dt.NewRow();
if (number == 0)
{
dt.Columns.Add("文件名");

}
number++;
dr["文件名"] = Path.GetFileName(path);

Dictionary<string, string> dic = new Dictionary<string, string>();
string[] str = DataInfo.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

for (int i = 0; i < str.Length; i++)
{
if (str[i].ToString() == "")
{
continue;

}


//if (!dic.ContainsKey(str[i].Substring(0, (str[i].IndexOf(':')))))
//{
//添加判断 键不存在 在添加
if (!dic.ContainsKey(str[i].Substring(0, (str[i].IndexOf(':')))))
dic.Add(str[i].Substring(0, (str[i].IndexOf(':'))), str[i].Substring((str[i].IndexOf(':') + 1), str[i].Length - str[i].IndexOf(':') - 1));
//}

}

foreach (var item in dic)
{

if (number1 == 0)
{
dt.Columns.Add(item.Key, Type.GetType("System.String"));


}

dr[item.Key] = item.Value;

}

dt.Rows.InsertAt(dr, count);

number1++;

count++;



// ds.Tables.Add(dt);
// dataGridView1.Rows.Add(new DataGridView(str));


return dt;
}



读书人网 >C#

热点推荐