从列表中获取数据到数据网格视图

Getting data from a list into a 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;
        }

批量购买class

    public class BulkPurchase : Product
    {
        private int quantity;
        
        public BulkPurchase(string prodName, float prodPrice, int prodQuantity)
            : base(prodName, prodPrice)
        {
            this.quantity = prodQuantity;
        }
    }

产品Class

    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 都没有帮助。

我不确定这是否是您的意思。 您需要做的第一件事是定义一个 public 产品名称 属性、一个 public 价格 属性 和一个 public 数量 属性 . 这些属性是可读和可写的属性。 这样控件就可以访问class.

之外的那些私有域了

我的修改如下:

//在demo中直接在列表中手动添加示例

//Write three data as an example
           cart.Add(new BulkPurchase("apple",10,1));
           cart.Add(new BulkPurchase("banana", 20, 1));
           cart.Add(new BulkPurchase("orange", 10, 1));

//产品

public class Product {
            private string name;
            private float price;

            public string ProductName {
                get {
                    return name;
                }
                set {
                    name = value;
                }
            }    
            public float Price {
                get {
                    return price;
                }
                set {
                    price = value;
                }
            }

//批量购买

public class BulkPurchase : Product {
            private int quantity;

            public BulkPurchase(string prodName, float prodPrice, int prodQuantity)
                : base(prodName, prodPrice) {
                Quantity = prodQuantity;
            }
           public int Quantity {
                get {
                    return quantity;
                }
                set {
                    quantity = value;
                }
            }
        }

//显示总金额

float TotalPrice = 0;
foreach (var item in cart) {
    TotalPrice += (item.Price*item.Quantity);
}
pnlCheckout.Text = $"Total $ {TotalPrice}";

//把数量放在第三列。

dataGridView1.DataSource = cart;
dataGridView1.Columns[0].DisplayIndex = 2;