不管是用 WinForm 的 DataGrid 还是用 WebForm 的 DataGrid,当用 DataTable 作数据源时,可以通过
DataRow row = myTable.Rows[i];
object val = row["ColumnName"];
来得到一行记录中的指定字段的信息, DataGrid 应该也是通过传入一个列名的字符串来取得这个值进行显示。但是如果我用自定义的类的对象数组作数据源时,类中也提供类似于 DataRow 的索引方法:
public class Student{
public object this[String column]{
return ......;
}
//......
}
时,DataGrid 却报错说没有这个属性,它只认识已经定义好的 get 属性。这是为什么?
DataRow row = myTable.Rows[i];
object val = row["ColumnName"];
来得到一行记录中的指定字段的信息, DataGrid 应该也是通过传入一个列名的字符串来取得这个值进行显示。但是如果我用自定义的类的对象数组作数据源时,类中也提供类似于 DataRow 的索引方法:
public class Student{
public object this[String column]{
return ......;
}
//......
}
时,DataGrid 却报错说没有这个属性,它只认识已经定义好的 get 属性。这是为什么?
public class Student{
public object this[String column]{
return ......;
}不用上面的索引。只能绑定属性(get/set的那个),不能绑定字段!
http://www.nikhilk.net/DataBindingToPublicFields.aspx