请问如何把一个DataTable中的两列值放入到哈希表中?
小弟初涉软坛,请多关照!o(∩_∩)o...

解决方案 »

  1.   


    对于列该没有直接的方法来处理
    定位至两列、逐字段添加, 建议Dictionary<>
      

  2.   

    我现在是用foreach循环把值添加到了哈希表中,不知Dictionary <>应该如何实现?
      

  3.   

          Dictionary<string, string> hastable = new Dictionary<string, string>();
                for (; ; )
                {
                    hastable.Add(keyname,value);
                }
      

  4.   

    我也看HASTABLE和DICTIONARY  刚比着别人博客做的练习 你看下。Console.WriteLine("hastable练习");
                Hashtable ht = new Hashtable();
                ht.Add(0, "1");
                ht.Add(1, "2");
                ht.Add(2, "string");
                //獲得HashTable中每個Value值1
                foreach (string var in ht.Values)
                {
                    //將獲得每個Value值賦給sHtValue
                    string sHtValue = var;
                }
                //獲得HashTable中每個Value值2
                for (int i = 0; i < ht.Count; i++)
                {
                    Console.WriteLine(ht[i]);
                }
                Console.WriteLine("///////////////////////////////////////////////////////////");            Console.WriteLine("Directionary练习");            Dictionary <int,string> dct=new Dictionary<int,string>();
                dct.Add(0, "a1");
                dct.Add(1, "a2");
                //獲取key值方法1
                foreach (int var in dct.Keys)
                {
                    int i = var;
                }
                //獲取key值方法2:用Keys集合轉移到ArrayList,用ArrayList轉出
                Console.WriteLine("//獲取key值方法2:用Keys集合轉移到ArrayList,用ArrayList轉出");
                ArrayList al = new ArrayList(dct.Values );
                //或者根据KEY
                //ArrayList al = new ArrayList(dct.Keys);
                for (int j = 0; j < al.Count; j++)
                {
                    Console.WriteLine(al[j]);
                }            Console.WriteLine("循環得到Dictionary中value的值");
                //循環得到Dictionary中value的值
                for (int iDCount = 0; iDCount < dct.Count; iDCount++)
                {
                    //將獲得每個Value值賦給sDirectionaryValue
                    Console.WriteLine( dct[iDCount]);
                }