I am not sure is this a best way but i am storing shopping cart data in Dictionary. I would like to bind data to gridview and use in aspx page.
Somehow, can we use <%# Eval("product_id") %>
in gridview in aspx page?
Set Dictionary
public Dictionary<int, Product> products = null;
Cart:
products = new Dictionary<int, Product>();
Reading data with this code:
Cart mycart = (Cart)Session["activecart"];
double GeneralTotal = 0;
foreach (int i in mycart.products.Keys)
{
Response.Write(mycart.products[i].product_id);
Response.Write(mycart.products[i].product_name);
Response.Write(mycart.products[i].product_price);
Response.Write(mycart.products[i].product_quantity);
Response.Write(mycart.products[i].total_price);
GeneralTotal += mycart.products[i].total_price;
}
Response.Write(GeneralTotal);
I can read values using foreach but i want to show those data in gridview using Eval.
I have tried this code to bind gridview:
Cart activeCart = (Cart)HttpContext.Current.Session["activecart"];
gridview1.DataSource = activeCart.products;
gridview1.DataBind();
This is only getting key, value variable from dictionary. Couldn't get specific values using <%#Eval("custom") %>
. Any suggestion?
I would do it like this:
Cart mycart = (Cart)Session["activecart"];
double GeneralTotal = 0;
DataTable tempBasket = new DataTable();
DataRow rowTempBasket;
tempBasket.Columns.Add("product_id", typeof (int));
//Add the other columns the same way
foreach (int i in mycart.products.Keys)
{
rowTempBasket = tempBasket.NewRow();
rowTempBasket["product_id"] = mycart.products[i].product_id;
//Add the other columns the same way
tempBasket.Rows.Add(rowTempBasket);
}
//Then DataBind the created table to your GridView
gridview1.DataSource = tempBasket;
gridview1.DataBind();
Then you should be able to use Eval.
EDIT
Maybe you post the code of your ASPX page. Would help what to do about the Total.