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

时间:2022-06-02 20:51:59

I'm looking to load a GridView with a generic list and have the columns be auto-generated. I am getting an exception that it does not have the correct properties to allow it to auto-generate the columns.

我正在寻找一个带有通用列表的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>

Page Load

页面加载

    //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

Student Class

学生班

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

Any thoughts or suggestions are appreciated, feel free to let me know if I'm going about this completely wrong.

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

1 个解决方案

#1


22  

Try adjusting your student class and change your fields into properties like this:

尝试调整您的学生班级并将您的字段更改为以下属性:

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

#1


22  

Try adjusting your student class and change your fields into properties like this:

尝试调整您的学生班级并将您的字段更改为以下属性:

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