C# 使用按钮将项目添加到列表

C# 使用按钮将项目添加到列表,c#,winforms,winapp,C#,Winforms,Winapp,我有一个windows应用程序,可以作为购物车使用。当双击列表中的项目时,客户将要购买的金额放入文本框,系统将该项目添加到临时列表中。如果该项目与购物车中的一个项目相似,系统将计算并修改购物车,而无需添加更多行。我现在可以添加更多类似于购物车中的项目的项目,但我不能在列表中添加更多行 private void btnAdd_Click(object sender, EventArgs e) { var obj = sCart.FirstOrDefault(x =>

我有一个windows应用程序,可以作为购物车使用。当双击列表中的项目时,客户将要购买的金额放入文本框,系统将该项目添加到临时列表中。如果该项目与购物车中的一个项目相似,系统将计算并修改购物车,而无需添加更多行。我现在可以添加更多类似于购物车中的项目的项目,但我不能在列表中添加更多行

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;


    }
从评论中:

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; } 
} 
这就是围巾的类型

List<Cart> sCart = new List<Cart>();
List sCart=新列表();
我可以添加第一项。如果我继续添加相同的项目(比如说,更新要购买的项目数量),它会起作用。但如果我再添加一项,什么也不会发生。gridview支持更新更多行,但是,只有我之前添加的第一项。我找不到问题出在哪里


p/s:感谢您向我展示如何发布问题。

修改您的类声明

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;
        }
    }

} 
类购物车:IEquatable
{ 
公共整数pID
{get;set;}
公共字符串pName
{get;set;}
公共字符串pDesc
{get;set;}
公共int-pPrice{get;set;}
公共int pAmount{get;set;}
公共整数pTotal{get;set;}
//建造师
公共购物车(int\U pid)
{
pID=_-pID;
}
公共布尔等于(其他)
{
if(this.pID==other.pID)
{
返回true;
}
其他的
{
返回false;
}
}
} 

在列表中添加类的代码

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();    
}
List sCart=新列表();
BindingSource=新的BindingSource();
私有void frmForm_加载(对象发送方,事件参数e)
{
source.DataSource=sCart;
this.gvCart.DataSource=源;
}
私有void btnAdd_单击(对象发送者,事件参数e)
{
购物车=新购物车(ProductID);
如果(sCart.Contains(购物车)=false)
{
//cart.pID=Convert.ToInt32(productID);
cart.pName=txtProName.Text;
cart.pDesc=txtDesc.Text;
cart.pPrice=Convert.ToInt32(lblDisplayPrice.Text);
cart.pAmount=Convert.ToInt32(txtmount.Text);
cart.pTotal=Convert.ToInt32(lblDisplayPrice.Text)*Convert.ToInt32(txtmount.Text);
添加(购物车);
}
其他的
{
购物车=围巾[围巾索引(购物车)];
cart.pAmount=cart.pAmount+Convert.ToInt32(txtmount.Text);
cart.pTotal=cart.pAmount*cart.pPrice;
}
source.CurrencyManager.Refresh();
}

对象sCart的类型是什么?List sCart=new List();欢迎来到堆栈溢出。请尽快阅读这一页。另外,请编辑您的问题以澄清它(而不仅仅是评论),特别是当它涉及代码时。我已将该类复制到问题中,但您应该将评论中的任何其他相关材料复制到问题中。很好,我可以向列表中添加更多项目,但当我添加相同项目时,会显示更多行,而不是增加现有项目的金额值。顺便说一句,在Cart Cart=new Cart(ProductID)时,ProductID到底是什么?您是否更新了由
IEquatable
接口继承的Cart类?我已经解决了,yayyyy。我用这个.gvCart.DataSource=null尝试自己的代码。非常感谢你。你的代码真的很有趣,你能解释一下那里发生了什么吗?因为CurrencyManager在更改DataSource中的值后不会刷新。因此,您必须手动刷新CurrencyManager。'Cart Cart=new Cart(ProductID)'我不太理解这里的ProductID,而不是说当前上下文中没有ProductID。