Gridview使用通用列表作为DataSource和自动生成列

时间:2010-11-11 21:07:55

标签: c# linq data-binding gridview

我正在寻找带有通用列表的GridView,并自动生成列。我得到一个例外,它没有正确的属性来允许它自动生成列。

异常

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;
}

任何想法或建议都表示赞赏,如果我完全错误,请随时告诉我。

1 个答案:

答案 0 :(得分:24)

尝试调整student课程,并将字段更改为以下属性:

public class student
{
   public string name { get; set; }
   public string address { get; set; }
}