使用通用列表作为数据源和自动生成列的 Gridview [英] Gridview using a generic list as DataSource and Auto-generating columns

查看:67
本文介绍了使用通用列表作为数据源和自动生成列的 Gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望使用通用列表加载 GridView 并自动生成列.我得到一个例外,它没有正确的属性来允许它自动生成列.

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.

例外

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.

网格视图

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

感谢任何想法或建议,如果我完全错误,请随时告诉我.

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

推荐答案

尝试调整您的 student 类并将您的字段更改为如下属性:

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

这篇关于使用通用列表作为数据源和自动生成列的 Gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆