0

我正在开发一个项目,在该项目中我将自定义列表绑定到数据网格。我认为我当前的应用程序可能存在一些问题,所以我决定在 4.0 中制作一个新的示例应用程序,但同样的问题。

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form` 
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void populate()
        {
            List<BillInfoCustom> oSalesList = new List<BillInfoCustom>();

            BillInfoCustom oSalesType = new BillInfoCustom() { BillId = 1, BillDate = DateTime.Now.ToLongDateString(), CashPayment = 10, CreditCardPaymet = 20, CustomerName = "asda", TotalPrice = 20.0 };

            BillInfoCustom oSalesType2 = new BillInfoCustom() { BillId = 1, BillDate = DateTime.Now.ToLongDateString(), CashPayment = 10, CreditCardPaymet = 20, CustomerName = "asda", TotalPrice = 20.0 };

            BillInfoCustom oSalesType3 = new BillInfoCustom() { BillId = 1, BillDate = DateTime.Now.ToLongDateString(), CashPayment = 10, CreditCardPaymet = 20, CustomerName = "asda", TotalPrice = 20.0 };

            oSalesList.Add(oSalesType);
            oSalesList.Add(oSalesType2);
            oSalesList.Add(oSalesType3);

            dataGridView1.DataSource = oSalesList;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            populate();
        }
    }

    public class BillInfoCustom
    {
        public int BillId;
        public double TotalPrice;
        public double CashPayment;
        public double CreditCardPaymet;
        public string CustomerName;
        public string BillDate;
    }
}

这是我按下按钮后的输出:

输出

4

1 回答 1

1

数据绑定适用于属性,而不是字段。您添加{ get; set; }到每个成员。尝试更改为:

public class BillInfoCustom
{
    public int BillId { get; set; }

    public double TotalPrice{ get; set; }

    public double CashPayment { get; set; }

    public double CreditCardPaymet { get; set; }

    public string CustomerName { get; set; }

    public string BillDate { get; set; }
}
于 2013-03-28T08:22:22.253 回答