Search code examples
c#listvisual-studiodatagridviewevent-driven

Getting data from a list into a datagridview


I'm creating a event-driven shopping cart and as a result I'm getting it to display in a datagridview but it's first of all storing the information in a list and from that list it's going to the datagridview when I click the Checkout Button.

What then happens is the columns and rows are generated but then it also displays the rows but the data isn't visible only the rows a columns.

this is what's it's like at the moment.

//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 class

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

Product 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

I'm completely stumped in how to solve this issue and all the Googling and YouTube doesn't sadly help.


Solution

  • I am not sure if this is what you mean. The first thing you need to do is to define a public product name property, a public price property, and a public quantity property. The properties are readable and writable properties. In this way, the control can access those private fields outside the class.

    My modification is as follows:

    //Manually add examples directly to the list in the 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));
    

    //Product

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

    //BulkPurchase

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

    //Display the total amount.

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

    //Put quantity in the third column.

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

    enter image description here enter image description here