Hashtable ht = new Hashtable();
        ht.Add(1, 单价 + 货号 + 尺寸);GridView1.DataSource = ht.  //我要gridview1 添加ht的 表数据 我该如何操作 
用不用 dataset?? 

解决方案 »

  1.   

    给个参考://更新购物车
        protected void lnkbtnUpdate_Click(object sender, EventArgs e)  
        {
            hashCar = (Hashtable)Session["ShopCart"];       //获取其购物车
            foreach (GridViewRow gvRow in gvShopCart.Rows)
            {
                TextBox tb = (TextBox)gvRow.FindControl("txtNum");
                int count = Int32.Parse(tb.Text);        //获取商品数量
                int GoodsID =Convert.ToInt32(gvRow.Cells[2].Text);    //获取商品ID
                hashCar[GoodsID] = count;
            }
            Session["ShopCart"] = hashCar;//更新购物车
            ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "click", "<script language='javascript'>window.location.href=window.location.href;</script>", false);
         }
      

  2.   

    参考:            Hashtable hsCodes = new Hashtable();            hsCodes.Add("key1", "value1");
                hsCodes.Add("key2", "value2");
                hsCodes.Add("key3", "value3");
                hsCodes.Add("key4", "value4");            DataTable dt = new DataTable();
                dt.Columns.Add(new DataColumn("key", System.Type.GetType("System.String")));
                dt.Columns.Add(new DataColumn("value", System.Type.GetType("System.String")));            //取得hsCodes中的数据放到DataTable中
                IDictionaryEnumerator myEnumerator = hsCodes.GetEnumerator();
                while (myEnumerator.MoveNext())
                {
                    dt.Rows.Add(new string[] { Convert.ToString(myEnumerator.Key), Convert.ToString(myEnumerator.Value) });
                } 
                this.dataGridView1.DataSource = dt;
      

  3.   

    怎么把哈希表里的数据 添加到DataTable表结构  之后 GridView1.DataSource = dt 显示出来就好了!
      

  4.   


    估计你最后少了句 
    GridView1.DataBind();
      

  5.   

      protected void GridView2_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            MultiView1.ActiveViewIndex = 9;
            string cs, gg, cc, qc, sj;
            cs = GridView2.Rows[e.NewSelectedIndex].Cells[2].Text.ToString();
            gg = GridView2.Rows[e.NewSelectedIndex].Cells[3].Text.ToString();
            cc = GridView2.Rows[e.NewSelectedIndex].Cells[4].Text.ToString();
            qc = GridView2.Rows[e.NewSelectedIndex].Cells[5].Text.ToString();
            Hashtable ht = new Hashtable();
            ht.Add(1, cs + gg + cc + qc);
            SqlDataAdapter sda = new SqlDataAdapter(
               这里面我不知道怎么写 应该是ht的数据吧);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            GridView1.DataSource = sda;
            GridView1.DataBind();
            
        }
      

  6.   

    这个问题好办:以下代码供参考,是某一正在使用的B2C商城的代码DataTable dtTable;
        Hashtable hashCar;//设置购物车内容的数据源
                    dtTable = new DataTable();
                    DataColumn column1 = new DataColumn("No");        //序号列
                    DataColumn column2 = new DataColumn("GoodsID");    //商品代号
                    DataColumn column3 = new DataColumn("GoodsName");  //商品名称
                    DataColumn column4 = new DataColumn("Num");       //数量
                    dtTable.Columns.Add(column1);  //添加新列            
                    dtTable.Columns.Add(column2);
                    dtTable.Columns.Add(column3);
                    dtTable.Columns.Add(column4);
                  
    DataRow row;
                    //对数据表中每一行进行遍历,给每一行的新列赋值
                    foreach (object key in hashCar.Keys)
                    {
                        row = dtTable.NewRow();
                        row["GoodsID"] = key.ToString();
                        row["Num"] = hashCar[key].ToString();
                        dtTable.Rows.Add(row);
                    }foreach (DataRow drRow in dtTable.Rows)
                    {
                        strSql = "select * from View_GT where GoodsID=" + Convert.ToInt32(drRow["GoodsID"].ToString());
                        DataSet ds = Base.GetDataSet(strSql, "tb");
                        DataRowView row1 = ds.Tables["tb"].DefaultView[0];                    drRow["No"] = i;                   //序号
                        drRow["GoodsName"] = row1["GoodsName"].ToString();  //名称
                        
                       }
    gvShopCart.DataSource = dtTable.DefaultView;   //绑定GridView控件
    gvShopCart.DataKeyNames = new string[] { "GoodsID" };
    gvShopCart.DataBind();
      

  7.   

    按照wangxiaofeiwuqiao朋友写的还是做不出来 报错了!“ 名为“2”的列已属于此 DataTable。”
    看来这个问题虽然简单 还是不太好解决啊!
      

  8.   

    而且 wangxiaofeiwuqiao 朋友提供的代码没有用到 哈希表 数据无法保存
      

  9.   


    这里已经用到哈希表数据//对数据表中每一行进行遍历,给每一行的新列赋值
                    foreach (object key in hashCar.Keys)
                    {
                        row = dtTable.NewRow();
                        row["GoodsID"] = key.ToString();
                        row["Num"] = hashCar[key].ToString();
                        dtTable.Rows.Add(row);
                    }