Creating an Online Shopping Cart and PayPal System - An Important Function
(Page 4 of 4 )
Now is a good time to open the main class file titled "site.aspx.cs" inside the "cs" folder and locate the function called "gvBasket_RowDataBound." Here's the function on its own, although you should check out the rest of the file in your own time:
protected void gvBasket_RowDataBound(object sender, GridViewRowEventArgs e)
{
switch( e.Row.RowType )
{
case DataControlRowType.DataRow:
// Name/Desc
((Literal)e.Row.FindControl("litItemName")).Text = Convert.ToString(((DataRowView)e.Row.DataItem)["name"]);
// Quantity
string quantity = Convert.ToString(((DataRowView)e.Row.DataItem)["quantity"]);
((HtmlInputText)e.Row.FindControl("itProductQuantity")).Value = quantity;
// Price
((Literal)e.Row.FindControl("litPrice")).Text = String.Format("{0:C2}", Convert.ToDouble(((DataRowView)e.Row.DataItem)["price"]) * Convert.ToInt32(quantity));
break;
case DataControlRowType.Footer:
DataTable dtShop = getBasketDt();
double total = 0.00;
for(int i = 0; i < dtShop.Rows.Count; i++)
{
total += Convert.ToInt32(dtShop.Rows[i]["quantity"]) * Convert.ToDouble(dtShop.Rows[i]["price"]);
}
((Literal)e.Row.FindControl("litTotalQuantity")).Text = Convert.ToString(dtShop.Compute("SUM(quantity)", ""));
((Literal)e.Row.FindControl("litDeliveryPrice")).Text = String.Format("{0:C2}", Convert.ToDouble(calcDeliveryCost(total)));
((Literal)e.Row.FindControl("litTotalPrice")).Text = String.Format("{0:C2}", Convert.ToDouble(calcDeliveryCost(total)) + total);
break;
}
}
The first thing we do is perform a switch on the RowType property so we can tell whether we are populating a Header, Footer or Item Template as all three will be separate calls to this same function. For both the product and basket we catch the DataControlRowType.DataRow row type as this is our ItemTemplate.
Because we gave all our controls unique IDs on the HTML page we can use the row's FindControl function. This will return an "Object" if any of the controls inside the row have a matching ID. We cast that to the type of object we're expecting, for example a "Literal" or a "HtmlInputText" field, and then assign the data to it via its TextorValue property. Each time a row is bound it gets passed into the function via the GridViewRowEventArgs.Row property. Using that we can access the row's DataItem, which contains all the data for that row from the DataSource. It's then up to us to decide which data we want out of that and how we shall use it.
In the basket we pull the name, quantity and price columns out of the DataItem and assign them to the relevant controls we embedded within the ItemTemplate. It's much the same for the DataControlRowType.Footer except that we pull a copy of the DataSource out of Session state ( getBasketDt(); ) since we want to generate the total cost and delivery cost using information from all of the rows, and not just the single row that got passed into the function.
Conclusion
Hopefully this article has shown you the basics of using a GridView as well as a few other tips and tricks along the way. We looked at one method of implementing the GridView and how we can have complete control over how its contents are generated. The next installment in this series will look into where the data comes from and take you through setting up the actual product/basket page.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |