【问题标题】:Unable to bind my List to datagridView [duplicate]无法将我的列表绑定到 datagridView [重复]
【发布时间】:2013-03-18 12:16:24
【问题描述】:

我正在处理一个项目,在该项目中我将自定义列表绑定到数据网格。我认为我当前的应用程序可能存在一些问题,所以我决定在 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;
    }
}

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

【问题讨论】:

  • 不是,也试过这个。我的情况与您提到的问题不同

标签: c# winforms list datagridview


【解决方案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; }
}

【讨论】:

  • 成功了。所以作为你在 SO 上的第一个回答者
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-20
  • 1970-01-01
  • 2014-01-17
  • 2011-05-07
  • 2011-05-30
  • 2014-08-18
相关资源
最近更新 更多