I have one windows app which works as a shopping cart. When double click to item in the list, customer put the amount he want to buy into textbox and the system will add that item into temporary list. If that item similar to one of the item in cart, the system will calculate and modify the cart without adding more row. I now can add more item which is similar to the item in cart, but I can't add more row into the list.
private void btnAdd_Click(object sender, EventArgs e)
{
var obj = sCart.FirstOrDefault(x => x.pID == Convert.ToInt32(productID));
if (obj == null)
{
sCart.Add(
new Cart()
{
pID = Convert.ToInt32(productID),
pName = txtProName.Text,
pDesc = txtDesc.Text,
pPrice = Convert.ToInt32(lblDisplayPrice.Text),
pAmount = Convert.ToInt32(txtAmount.Text),
pTotal = Convert.ToInt32(lblDisplayPrice.Text) * Convert.ToInt32(txtAmount.Text)
}
);
}
else {
obj.pAmount = obj.pAmount + Convert.ToInt32(txtAmount.Text);
obj.pTotal = obj.pAmount * obj.pPrice;
}
this.gvCart.DataSource = sCart;
}
From the comments:
class Cart
{
public int pID
{ get; set; }
public string pName
{ get; set; }
public string pDesc
{ get; set; }
public int pPrice { get; set; }
public int pAmount { get; set; }
public int pTotal { get; set; }
}
This is the type of sCart.
List<Cart> sCart = new List<Cart>();
I can add the first item. If I continue to add the same item (let say, update amount of that item to buy), it works. But if I add another item, nothing happen. The gridview support to update more row but, there is only the first item I added before. I couldn't find where the problem was...
p/s: thanks for showing me how to post a question.