我正在寻找一个带有通用列表的GridView并自动生成列。我遇到一个异常(exception),它没有正确的属性,无法自动生成列。
异常
The data source for GridView with id 'GV1' did not have any properties or attributes from which to generate columns. Ensure that your data source has content.
GridView
<asp:GridView ID="GV1" runat="server" AutoGenerateColumns="true"></asp:GridView>
页面加载
//LINQ query to populate list
List<student> su = new List<student>();
dbDataContext db = new dbDataContext();
var q = from c in db.data_table
where c.processed == false
orderby c.date_complete descending
select c;
//iterate through results and add to list
foreach(var c in q)
{
student s = new student { name = c.name, address = c.address };
su.Add(s);
}
//Load GridView
GV1.DataSource = su;
GV1.DataBind(); //Exception thrown here
学生类
public class student
{
public string name;
public string address;
}
任何想法或建议都值得赞赏,如果我要彻底解决这个问题,请随时告诉我。
最佳答案
尝试调整student
类,并将字段更改为如下属性:
public class student
{
public string name { get; set; }
public string address { get; set; }
}