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.)
GridView Markup:
<asp:GridView ID="gvCart" runat="server" CssClass="pList" AutoGenerateColumns="false" DataKeyNames="ProductID">
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ProductID" />
<asp:BoundField DataField="Name" HeaderText="ProductName" />
<asp:ImageField DataImageUrlField="Thumbnail" HeaderText="Thumbnail"></asp:ImageField>
<asp:BoundField DataField="Unit Price" HeaderText="Unit Price" />
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="Quantity" runat="server" Text="<%# Bind('Quantity') %>" Width="25px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Total Price" HeaderText="Total Price" />
</Columns>
</asp:GridView>
DataTable Code-Behind:
private void View(List<OrderItem> 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 < gvCart.Rows.Count; counter++)
{
gvCart.Rows[counter].Cells.Add(Common.createCell("<a href='cart.aspx?action=update&prodId=" +
gvCart.Rows[counter].Cells[0].Text + "'>Update</a><br /><a href='cart.aspx?action='action=remove&prodId=" +
gvCart.Rows[counter].Cells[0].Text + "/>Remove</a>"));
}
}
}
Error occurs below in the foreach - the GridViewRowCollection is empty!
private void Update(string prodId)
{
List<OrderItem> cart = (List<OrderItem>)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);
}
}
}
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.