【问题标题】:Getting data from a list into a datagridview从列表中获取数据到 datagridview
【发布时间】: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,如果有帮助的话,我已经添加了其他两个类。
  • 我认为您需要做的第一件事是切换到属性,而不是使用getset方法...公共属性将用于在收集时检索数据以用于显示目的绑定到 GridView、ListView、ComboBox 等控件。

标签: c# list visual-studio datagridview event-driven


【解决方案1】:

我不确定这是否是您的意思。 您需要做的第一件事是定义公共产品名称属性、公共价格属性和公共数量属性。 属性是可读和可写的属性。 这样,控件就可以访问类外的那些私有字段了。

我的修改如下:

//手动将示例直接添加到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;

【讨论】:

  • 非常感谢您的帮助,这让我彻底理解了它。
猜你喜欢
  • 1970-01-01
  • 2015-09-09
  • 1970-01-01
  • 2011-07-31
  • 2017-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多