【发布时间】:2023-03-15 19:35:02
【问题描述】:
我正在创建一个事件驱动的购物车,因此我将它显示在 datagridview 中,但它首先将信息存储在一个列表中,当我点击时,它会从该列表中转到 datagridview结帐按钮。
然后发生的是生成列和行,但随后它也显示行,但数据仅显示行和列。
这就是现在的样子。
//defines the list and links to the where the product information is and when a product is added to the cart.
public static List<BulkPurchase> cart = new List<BulkPurchase>();
private void btnCheckout_Click(object sender, EventArgs e)
{
//makes the checkout visible
pnlMain.Visible = false;
//makes the products not visible
pnlCheckout.Visible = true;
//just shows me that items have been added to the list
foreach (var item in cart)
{
Console.WriteLine(item.getName());
Console.WriteLine(item.getPrice());
}
//inputs the cart to the datagridview
dataGridView1.DataSource = cart;
}
BulkPurchase 类
public class BulkPurchase : Product
{
private int quantity;
public BulkPurchase(string prodName, float prodPrice, int prodQuantity)
: base(prodName, prodPrice)
{
this.quantity = prodQuantity;
}
}
产品类别
public class Product
{
private string name;
private float price;
//the constructor for a product
public Product (string prodName, float prodPrice)
{
this.name = prodName;
this.price = prodPrice;
}
//accessing the private variable of name
public string getName() => name;
//accessing the private variable of price
public float getPrice() => price;
}
When I add three items to the list this is the outcome of the datagridview
我完全不知道如何解决这个问题,而且所有的谷歌搜索和 YouTube 都没有帮助。
【问题讨论】:
-
你需要展示
BulkPurchase的样子。美元到甜甜圈你有字段而不是属性 -
好的,感谢@ŇɏssaPøngjǣrdenlarp,如果有帮助的话,我已经添加了其他两个类。
-
我认为您需要做的第一件事是切换到属性,而不是使用
get和set方法...公共属性将用于在收集时检索数据以用于显示目的绑定到 GridView、ListView、ComboBox 等控件。
标签: c# list visual-studio datagridview event-driven