2013-12-13 98 views
2

現在是2013年12月13日。無法綁定列表<T>到C#中的DataGridView!

我有一個列表<>要綁定到DataGridView。 它的工作,但今天它不工作。

public class ExamResult 
{ 
    public string ID; 
    public bool Result; 
    public bool ReviwerResult; 
    public string QuestionFileName; 
} 

然後創建列表:

List<ExamResult> result = new List<ExamResult>(); 

創建ExamResult的實例和值分配給它的成員,然後將其添加到列表:

//Create a Instance of ExamResult: 
ExamResult examResult = new ExamResult(); 

//Assign Value to members: 
examResult.ID="001"; 
examResult.Result=false; 
examResult.ReviewerResult=true; 
examResult.QuestionFileName = string.empty; 

//Add examResult Instance to List<ExamResult> 
result.Add(examResult); 

然後嘗試將其綁定到一個DataGridView在我的WinForm應用程序上。

this.DataGridView.AutoGenerateColumns = true; 
this.DataGridView.DataSource = result; 

但DataGridView不顯示任何東西!

我發誓此代碼工作,現在它不工作!而不改變代碼。

什麼問題?

+3

只要看看今天一天過,使它成爲一個長週末,回來在星期一,它應該是罰款 – Habib

+0

在一個嚴重的注意,這個代碼應該工作,嘗試清理/重建你的應用程序。重新啓動visual studio並確保在綁定之前沒有清除你的列表 – Habib

+0

你的意思是'它不工作'是什麼意思?是否有錯誤訊息?你會得到意想不到的結果?一點都沒有? – matt

回答

5

改變你的類,而是使用屬性字段:

public class ExamResult 
{ 
    public string ID {get; set;} 
    public bool Result {get; set;} 
    public bool ReviwerResult {get; set;} 
    public string QuestionFileName {get; set;} 
} 
+0

+1,看起來代碼已被更改,而不是星期五的第13個效果 – Habib

+0

爲什麼當將'字段'更改爲'自動屬性'時它的工作原理? –

+0

綁定正在尋找屬性,而不是字段。請參閱[字段和C#中的屬性之間的區別是什麼?](http://stackoverflow.com/questions/295104/what-is-the-difference-between-a-field-and-a-property-in -C) – LarsTech