2013-03-28 53 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