1

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.

1 Answers1

1

Modify your class declaration

class Cart : IEquatable<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; } 
    //Constructor
    public Cart(int _pid)
    {
        pID = _pid;
    }
    public bool Equals(Cart other)
    {
        if (this.pID == other.pID)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

} 

Code to add class in the list

List<Cart> sCart = new List<Cart>();
BindingSource source = new BindingSource();

private void frmForm_Load(object sender, EventArgs e)
{
    source.DataSource = sCart;
    this.gvCart.DataSource = source;
}
private void btnAdd_Click(object sender, EventArgs e)
{
    Cart cart = new Cart(ProductID);
    if (sCart.Contains(cart) == false)
    {
        //cart.pID = Convert.ToInt32(productID);
        cart.pName = txtProName.Text;
        cart.pDesc = txtDesc.Text;
        cart.pPrice = Convert.ToInt32(lblDisplayPrice.Text);
        cart.pAmount = Convert.ToInt32(txtAmount.Text);
        cart.pTotal = Convert.ToInt32(lblDisplayPrice.Text) * Convert.ToInt32(txtAmount.Text);
        sCart.Add(cart);
    }
    else
    {
        cart = sCart[sCart.IndexOf(cart)];
        cart.pAmount = cart.pAmount + Convert.ToInt32(txtAmount.Text);
        cart.pTotal = cart.pAmount * cart.pPrice;
    }

    source.CurrencyManager.Refresh();    
}
Shell
  • 6,818
  • 11
  • 39
  • 70
  • Beautiful, I can add more item to list, but when I add the same item, more row appear instead of increase the amount value of existed item. Btw, at Cart cart = new Cart(ProductID), what exactly is ProductID? – Jackie Ngo Anh Khoi Mar 01 '14 at 04:53
  • have you updated your Cart class inherited by `IEquatable` interface? – Shell Mar 01 '14 at 04:57
  • I solved it, yayyyy. I try my own code with this.gvCart.DataSource = null. Thank you very much. Your code is really interesting, can you explain me what happen there? – Jackie Ngo Anh Khoi Mar 01 '14 at 05:13
  • Because the CurrencyManager does not refresh after changing the value in DataSource. So, you have to refresh CurrencyManager manually. – Shell Mar 01 '14 at 05:50
  • 'Cart cart = new Cart(ProductID)' I don't really understand ProductID here, VS said there is no ProductID in current context. – Jackie Ngo Anh Khoi Mar 01 '14 at 06:04
  • @JackieNgoAnhKhoi You have declared productID not ProductID. So, you can change it to productID. – Shell Mar 01 '14 at 06:05
  • I tried already but it said there is no constructor that takes 1 argument – Jackie Ngo Anh Khoi Mar 01 '14 at 06:32
  • @JackieNgoAnhKhoi Sorry, there was a mistake in my code. I was missing Constructor in Cart class. I have updated my code. please check – Shell Mar 01 '14 at 06:34
  • Thank you for your kindness. It works now. But I couldn't fully understand all the code. – Jackie Ngo Anh Khoi Mar 01 '14 at 06:43