| Preview |
|
|
|
heres my interface. http://i279.photobucket.com/albums/kk137/wh3resmycar/gridview.jpg i can already extract serial column on my gridview and post it on the label (on this example its the label besides the button). now what i want to do after is after the user clicks the button "add product to invoice" i want to extract the whole selected row information and put it on a temporary table like some sort of a "cart" with emphasis on calculating the total price. how do i display it? |
Loading

Amit ChoudharyRank #145 · 15.8K reputationPosted Nov 25, 2009, 6:53 PM EST
Here is my cart code m sending to you and it is exactly the same you wanted with your grid....
and please mark the answer as I'm giving you the complete code for cart... :)
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Collections; using System.Collections.Specialized; using System.ComponentModel; using System.IO; using System.Text; using System.Text.RegularExpressions; using System.Web.Caching; using System.Web.SessionState; using System.Web.UI.HtmlControls; using System.Web.Mail; using System.Net; using System.Drawing; using System.Data.SqlClient; using EntitySpaces.Core; using EntitySpaces.Interfaces; using BusinessObjects; public partial class Product : System.Web.UI.Page { DataSet ds; ProductsCollection pc; protected void Page_Load(object sender, EventArgs e) { pc = BusinessObjects.Products.getallproducts(); pc.LowLevelBind(); } private DataTable createCartDT() { DataTable dtProducts = new DataTable(); DataColumn productColumn = new DataColumn(); productColumn.DataType = System.Type.GetType("System.Int32"); productColumn.ColumnName = "ProductId"; productColumn.Unique = true; dtProducts.Columns.Add(productColumn); productColumn = new DataColumn(); productColumn.DataType = System.Type.GetType("System.String"); productColumn.ColumnName = "ProductName"; dtProducts.Columns.Add(productColumn); productColumn = new DataColumn(); productColumn.DataType = System.Type.GetType("System.Double"); productColumn.ColumnName = "ProductPrice"; dtProducts.Columns.Add(productColumn); productColumn = new DataColumn(); productColumn.DataType = System.Type.GetType("System.Int32"); productColumn.ColumnName = "Quantity"; dtProducts.Columns.Add(productColumn); // Make "id" the primary key DataColumn[] pkColumns = new DataColumn[1]; pkColumns[0] = dtProducts.Columns["ProductId"]; dtProducts.PrimaryKey = pkColumns; return dtProducts; } protected void shopBuy_OnServerClick(object source, EventArgs e) { int index = ((GridViewRow)((HtmlInputImage)source).Parent.NamingContainer).RowIndex; // Fetch the productId using the RowIndex we got from the GridViewRow addToCart((Convert.ToInt32(gvProducts.DataKeys[index].Value))); } protected void addToCart(int Pid) { DataTable dtCart = getCartDt(); bool found = false; for (int i = 0; i 0; } private DataTable getCartDt() { DataTable dtCart = null; if (Session["dtCart"] == null) { dtCart = createCartDT(); } else { dtCart = (DataTable)Session["dtCart"]; } return dtCart; } protected DataTable getProductDt() { return (DataTable)ds.Tables[0]; } protected void gvCart_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)["ProductName"]); // 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)["ProductPrice"]) * Convert.ToInt32(quantity)); break; case DataControlRowType.Footer: DataTable dtShop = getCartDt(); double total = 0.00; for (int i = 0; i /// This is a simple delivery cost calculator. It works off the basket total and not each item individually. /// In a more robust system each item would have its own delivery methods and delivery costs which would /// account for its dimensions, weight, particular requirements etc. /// private string calcDeliveryCost(double total) { string deliveryCost = "0.00"; if (total = 30.00 && total = 70.00 && total = 145.00) { deliveryCost = "0.00"; } return deliveryCost; } protected void shopClearCart_OnServerClick(object source, EventArgs e) { Session.Contents.Remove("dtCart"); updateCart(); } protected void shopUpdateCartQuantities_OnServerClick(object source, EventArgs e) { DataTable dtCart = getCartDt(); /* * Go through each row in the basket display and compare it to the basket in the session * Compare the quantities and modify the Session basket as required to reflect * any changes made by the customer via the quanitites input boxes */ foreach (GridViewRow row in gvCart.Rows) { int index = row.RowIndex; int productId = Convert.ToInt32(gvCart.DataKeys[index].Value); // Find the product in the Session Basket DataRow drProduct = dtCart.Rows.Find(productId); if (drProduct != null) { // Read the data from the Quantity Text Box HtmlInputText itQuant = (HtmlInputText)row.FindControl("itProductQuantity"); // Try and convert the value to an int try { int quant = Convert.ToInt32(itQuant.Value); /* * If the value converted to an Int successfully we still need to check * that its not a minus number, otherwise we might end up owing the customer money! */ if (quant > 0) { drProduct["Quantity"] = quant; } else { drProduct.Delete(); } } catch { // If we cant convert it to an Int then just leave it as it was i.e. make no changes } } } if (dtCart.Rows.Count > 0) { // IF we still have items in the basket then update the Session Basket Session["dtCart"] = dtCart; } else { // ELSE delete the Session Basket Session.Contents.Remove("dtCart"); } // Refresh the basket display updateCart(); } protected void gvProducts_PageIndexChanging(Object sender, GridViewPageEventArgs e) { gvProducts.PageIndex = e.NewPageIndex; gvProducts.DataSource = ds; gvProducts.DataBind(); } protected void shopCartCheckout_OnServerClick(object source, EventArgs e) { //put your check out code here } }Mark Uy130 reputationPosted Nov 26, 2009, 9:06 PM EST
anybody else who can help?
Mark Uy130 reputationPosted Nov 26, 2009, 9:18 PM EST
Amit ChoudharyRank #145 · 15.8K reputationPosted Nov 29, 2009, 8:45 PM EST
Hi.. friend, sorry for late reply but you are looking for this one i think.... if this is the answer then please mark it.... // The following fuction fetch the details from Gridview protected void shopBuy_OnServerClick(object source, EventArgs e) { int index = ((GridViewRow)((HtmlInputImage)source).Parent.NamingContainer).RowIndex; // Fetch the productId using the RowIndex we got from the GridViewRow addToCart((Convert.ToInt32(gvProducts.DataKeys[index].Value))); }protected void addToCart(int Pid) { DataTable dtCart = getCartDt(); bool found = false; for (int i = 0; i < dtCart.Rows.Count; i++) { if (Convert.ToInt32(dtCart.Rows[i]["ProductId"]) == Pid) { dtCart.Rows[i]["Quantity"] = Convert.ToInt32(dtCart.Rows[i]["Quantity"]) + 1; found = true; break; } } if (!found) { DataTable dtProduct = getProductDt(); DataRow drProduct = dtProduct.Rows.Find(Pid); DataRow newRow = dtCart.NewRow(); newRow["ProductId"] = drProduct["ProductId"] newRow["ProductName"] = drProduct["ProductName"]; newRow["ProductPrice"] = drProduct["ProductPrice"]; newRow["Quantity"] = 1; dtCart.Rows.Add(newRow); } Session["dtCart"] = dtCart; updateCart(); }Sam HobbsRank #107 · 20.5K reputationPosted Nov 30, 2009, 4:20 PM EST
Are you sure you don't need to save the data? It is very common that the contents of a shopping cart is saved for longer than a single session, so someone can continue shopping if they get interrupted. If your application can benefit from that, then it would help to include that in your current design.