winforms 如何将datagridview绑定到类中的数据

gblwokeq  于 2022-11-16  发布在  其他
关注(0)|答案(2)|浏览(161)

在C# Winform中将数据从类绑定到datagridview时遇到问题
我创建了一个3个类,分别是invoice类、SoldItem类和Item类,其中invoice类的公共成员是list,SoldItem类中有两个公共成员,分别是int quantitysold和Item itemsold
然后我有一个datagridview,它已经有了它所有列的名称。我想显示发票对象中的信息,几天后我仍然不知道如何做到这一点。
我试过这个:
lblDate.Text =发票.发票日期.ToString();客户.文本=发票.客户;但不知道如何将发票对象中数据与数据网格视图相关联。
下面是我的类定义、形式和函数。

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

uinbv5nw1#

请使用DataGridView数据源

nhaq1z21

nhaq1z212#

您可以定义DataGridView的DataSource属性。

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

this.dataGridView1.DataSource = invoice.ListSoldItem;

datagrid的列是由对象属性定义的。例如,要有一个列“Total”,你必须创建一个新的对象属性来返回价格合计。类似于:

class SoldItem
{
    [DisplayName("Quantity")]
    public int QuantitySold { get; set; }
    public Item ItemSold { get; set; }
    public SoldItem(int quantitySold, Item itemSold)
    {
        QuantitySold = quantitySold;
        ItemSold = itemSold;
    }

    public double? Total => QuantitySold * ItemSold.SellingPrice;
}

属性DisplayName更改列标题。
此外,要显示项的名称而不是WinFormsApp1.Item,必须通过添加一个方法来重写ToString(),从而修改Item类。

class Item
{
    public string? ItemName { get; set; }
    public double? SellingPrice { get; set; }
    public Item(string itemName, double sellingPrice)
    {
        ItemName = itemName;
        SellingPrice = sellingPrice;
    }

    public override string ToString()
    {
        return ItemName;
    }
}

结果:

相关问题