首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

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

2013-09-12 
dictionary已添加了具有相同键的项dictionary已添加了具有相同键的项,这个怎么搞啊[解决办法]dictionary是

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;
        }



热点排行