2013年9月11日星期三

dictionary has been added with the same key

dictionary has been added with the same key , how do ah
------ Solution - -------------------------------------------
dictionary is a dictionary , it does not allow duplicate key , you must be repeated to add the same key of value, so reported this error .
------ Solution ---------------------------------------- ----
 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;
        }



------ For reference only ---------------------------------- -----
this is the way , there is no problem ah
  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(':')))))
                //{
                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;
        }

------ For reference only ----------------------------------- ----
Thank you corner_hzd

没有评论:

发表评论