title: adding drop down list in gridview using c# in asp.net
body: <p>I'm currently working on my shopping cart. and I'm wondering how I can add drop down list column for quantity in every item that is inside the Cart. And also I want the limit of drop down list to be the number of the product stock. Hoping for a brief explanation. Thank you. :)</p>
  
  <p>Cart.aspx.cs</p>
  
  <pre><code>    protected void theCart()
      {        
  
          var dt = new DataTable();
          double totalPrice = 0;
          dt.Columns.Add("product_name");
          dt.Columns.Add("product_price");
          dt.Columns.Add("product_stock");
  
          List&lt;ShoppingCartModel&gt; shoppingList = (List&lt;ShoppingCartModel&gt;)Session["memberCart"];
  
          if (shoppingList != null)
          {
              foreach (var item in shoppingList)
              {
                  dt.Rows.Add(item.ProductName, item.Price, item.ProductStock);
  
                  totalPrice += item.Price;
              }
          }
          lbl_total.Text = totalPrice.ToString();
  
          MyGridView.DataSource = dt;
          MyGridView.DataBind();
      }
  </code></pre>
  
  <p>WebShop.aspx.cs</p>
  
  <pre><code>     protected void pro_tbl_ItemCommand(object source, DataListCommandEventArgs e)
      {
          if (e.CommandName == "addtocart")
          {
              List&lt;ShoppingCartModel&gt; shoppingList = (List&lt;ShoppingCartModel&gt;)Session["memberCart"];
  
              if (shoppingList == null)
              {
                  shoppingList = new List&lt;ShoppingCartModel&gt;();
              }
  
              ShoppingCartModel shoppingModel = new ShoppingCartModel();
  
              shoppingModel.ProductId = ((HiddenField)e.Item.FindControl("lblProductId")).Value;
              shoppingModel.ProductName = ((Label)e.Item.FindControl("lbl_pname")).Text;
              shoppingModel.ProductDescription = ((Label)e.Item.FindControl("lbl_pdesc")).Text;
              shoppingModel.ProductStock = Convert.ToInt32(((Label)e.Item.FindControl("lbl_pstock")).Text);
              shoppingModel.Price = Convert.ToDouble(((Label)e.Item.FindControl("lbl_price")).Text);
  
              shoppingList.Add(shoppingModel);
              Session["memberCart"] = shoppingList;
  
              lbl_result.Text = "Product has been added to the cart";
          }
      }
  </code></pre>