2013-03-28 66 views
0

我正在处理一个项目,其中我将自定义列表绑定到数据网格。我认为我目前的应用程序可能有一些问题,所以我决定在4.0中创建一个新的示例应用程序,但同样的问题。无法将我的列表绑定到datagridView

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

这里是输出后,我按按钮:在性能,而不是场

output

+0

它没有,也尝试过这一点。和我的情况是不同于你提到的问题 – DDR 2013-03-28 06:28:34

回答

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; } 
} 
+0

伎俩。所以你的第一个答案就是SO – DDR 2013-03-28 09:39:46