我正在开发一个项目,我将自定义列表绑定到数据网格。我以为我可能在当前的应用程序中遇到一些问题,所以我决定在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;
}
}
这是我按下按钮后的输出:
答案 0 :(得分: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; }
}