gpt4 book ai didi

c# - 使用通用列表作为数据源和自动生成列的 Gridview

转载 作者:可可西里 更新时间:2023-11-01 02:59:43 26 4
gpt4 key购买 nike

我希望加载一个带有通用列表的 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

学生类(class)

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

欢迎提出任何想法或建议,如果我的做法完全错误,请随时告诉我。

最佳答案

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

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

关于c# - 使用通用列表作为数据源和自动生成列的 Gridview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4159287/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com