2013-12-13 47 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