Home > Mobile >  How to bind datagridview to data in a class
How to bind datagridview to data in a class

Time:11-15

I have a problem to bind the data from class to datagridview in C# Winform

I created a 3 classes, it is invoice class, SoldItem class and Item class one of the public member of invoice class is a list. inside solditem, there are 2 public member named int quantitysold and Item itemsold

Then I have a datagridview which already has names for all of its column. I want to display the information from the invoice object, after few days I still could not figure out how to do that.

I have tried this:

lblDate.Text = invoice.InvoiceDate.ToString(); customer.Text = invoice.Customer; but dont know how to relate data from invoice object to datagridview.

Here is my class definitioan, form, and function.

class Item
    {
        public string? ItemName { get; set; }
        public double? SellingPrice { get; set; }
        public Item(string itemName, double sellingPrice)
        {
            ItemName = itemName;
            SellingPrice = sellingPrice;
        }
    }
    class SoldItem
    {
        public int QuantitySold { get; set; }
        public Item ItemSold { get; set; }
        public SoldItem(int quantitySold, Item itemSold) {
            QuantitySold = quantitySold;
            ItemSold = itemSold;
        }
    }
    class Invoice
    {
        public DateTime InvoiceDate{ get; set; }
        public string Customer { get; set; }
        public BindingList<SoldItem>? ListSoldItem { get; set; }
        public Invoice() { }
    }

// form
// 
// dgv
// 
this.dgv.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
    this.Item,
    this.Quantity,
    this.Price,
    this.LineTotal});


    this.Item.HeaderText = "Item Description";
    this.Item.Name = "Item";
    this.Quantity.HeaderText = "Quantity";
    this.Quantity.Name = "Quantity";
    this.Price.HeaderText = "Price";
    this.Price.Name = "Price";
    this.Price.ReadOnly = true;
    this.LineTotal.HeaderText = "Line Total";
    this.LineTotal.Name = "LineTotal";


private void setupForm() {

Invoice invoice = new Invoice();
            invoice.InvoiceDate = DateTime.Now;
            invoice.Customer = "Frank";
            invoice.ListSoldItem = new BindingList<SoldItem>()
            {
                new (2, new ("Item A", 10)),
                new (4, new ("Item B", 50)),
                new (7, new ("Item C", 150)),
                new (2, new ("Item D", 1000)),
                new (4, new ("Item E", 5)),
                new (7, new ("Item F", 15))
            };

            lblDate.Text = invoice.InvoiceDate.ToString();
            customer.Text = invoice.Customer;
}

CodePudding user response:

You can define the datasource of the datagridview.

this.dgv.DataSource = invoice.ListSoldItem

Columns of datagrid is defined by object properties. For example, to have a column "Total", you must create a new object property which return the price total. something like that:

public int Total => QuantitySold * ItemSold.SellingPrice;

CodePudding user response:

Can Please Use DataGridView DataSource

  • Related