title: Binding DataTable To GridView, But No Rows In GridViewRowCollection Despite GridView Population?
body: <p>Problem: I've coded a GridView in the markup in a page. I have coded a DataTable in the code-behind that takes data from a collection of custom objects. I then bind that DataTable to the GridView. (Specific problem mentioned a couple code-snippets below.)</p>
  
  <p>GridView Markup:</p>
  
  <pre><code>&lt;asp:GridView ID="gvCart" runat="server" CssClass="pList" AutoGenerateColumns="false" DataKeyNames="ProductID"&gt;
          &lt;Columns&gt;
              &lt;asp:BoundField DataField="ProductID" HeaderText="ProductID" /&gt;
              &lt;asp:BoundField DataField="Name" HeaderText="ProductName" /&gt;
              &lt;asp:ImageField DataImageUrlField="Thumbnail" HeaderText="Thumbnail"&gt;&lt;/asp:ImageField&gt;
              &lt;asp:BoundField DataField="Unit Price" HeaderText="Unit Price" /&gt;
              &lt;asp:TemplateField HeaderText="Quantity"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:TextBox ID="Quantity" runat="server" Text="&lt;%# Bind('Quantity') %&gt;" Width="25px"&gt;&lt;/asp:TextBox&gt;
                  &lt;/ItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
              &lt;asp:BoundField DataField="Total Price" HeaderText="Total Price" /&gt;
          &lt;/Columns&gt;
      &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>DataTable Code-Behind:</p>
  
  <pre><code>private void View(List&lt;OrderItem&gt; cart)
      {
          DataSet ds = new DataSet();
          DataTable dt = ds.Tables.Add("Cart");
  
          if (cart != null)
          {
              dt.Columns.Add("ProductID");
              dt.Columns.Add("Name");
              dt.Columns.Add("Thumbnail");
              dt.Columns.Add("Unit Price");
              dt.Columns.Add("Quantity");
              dt.Columns.Add("Total Price");
  
              foreach (OrderItem item in cart)
              {
                  DataRow dr = dt.NewRow();
  
                  dr["ProductID"] = item.productId.ToString();
                  dr["Name"] = item.productName;
                  dr["Thumbnail"] = ResolveUrl(item.productThumbnail);
                  dr["Unit Price"] = "$" + item.productPrice.ToString();
                  dr["Quantity"] = item.productQuantity.ToString();
                  dr["Total Price"] = "$" + (item.productPrice * item.productQuantity).ToString();
  
                  dt.Rows.Add(dr);
              }
  
              gvCart.DataSource = dt;
              gvCart.DataBind();
              gvCart.Width = 500;
  
              for (int counter = 0; counter &lt; gvCart.Rows.Count; counter++)
              {
                  gvCart.Rows[counter].Cells.Add(Common.createCell("&lt;a href='cart.aspx?action=update&amp;prodId=" +
                      gvCart.Rows[counter].Cells[0].Text + "'&gt;Update&lt;/a&gt;&lt;br /&gt;&lt;a href='cart.aspx?action='action=remove&amp;prodId=" +
                      gvCart.Rows[counter].Cells[0].Text + "/&gt;Remove&lt;/a&gt;"));
              }
          }
      }
  </code></pre>
  
  <p>Error occurs below in the foreach - <strong>the GridViewRowCollection is empty!</strong></p>
  
  <pre><code>private void Update(string prodId)
      {
          List&lt;OrderItem&gt; cart = (List&lt;OrderItem&gt;)Session["cart"];
          int uQty = 0;
  
          foreach (GridViewRow gvr in gvCart.Rows)
          {
              if (gvr.RowType == DataControlRowType.DataRow)
              {
                  if (gvr.Cells[0].Text == prodId)
                  {
                      uQty = int.Parse(((TextBox)gvr.Cells[4].FindControl("Quantity")).Text);
                  }
              }
          }
  </code></pre>
  
  <p>Goal: I'm basically trying to find a way to update the data in my GridView (and more importantly my cart Session object) without having to do everything else I've seen online such as utilizing OnRowUpdate, etc. Could someone please tell me why gvCart.Rows is empty and/or how I could accomplish my goal without utilizing OnRowUpdate, etc.? When I execute this code, the GridView gets populated but for some reason I can't access any of its rows in the code-behind.</p>