【问题标题】:How to bind List<class> object to gridview如何将 List<class> 对象绑定到 gridview
【发布时间】:2013-04-12 17:19:18
【问题描述】:

下面是我的代码

    List<test> Students = new List<test>(){
    new test() { name = "Jack", imgpath = "15", Des = "100" },
    new test() { name = "Smith", imgpath = "15", Des = "101" },       
    new test() { name = "Smit", imgpath = "1", Des = "102" }
};
GridView1.DataSource = Students;
GridView1.DataBind();

我的班级是:

public class test
{
    public string name;
    public string imgpath;
    public string Des;  
 }

但它给了我错误“在选定的数据源上找不到名为'name'的字段或属性。”

那么怎么解决呢。我不知道我的代码有什么问题。

谢谢

【问题讨论】:

  • 您需要从 ASPX 页面粘贴代码以进行 Gridview 声明。
  • 您在 Gridview 中使用 Eval。如果是这样,请确保字段名称。

标签: asp.net


【解决方案1】:

试试:

public string name { get; set };
public string imgpath { get; set };
public string Des { get; set };

【讨论】:

  • 因为我前段时间遇到了同样的问题,这为我解决了。
  • 我已收回我的投票,但不确定这是否会有所帮助
  • 我已经检查了这个东西,它对我来说是有问题的。
【解决方案2】:

无法绑定到字段,如前所述,请改用属性。

我看到你正在使用 ASP.net,目前找不到任何关于它的绑定的信息,但我认为它对 WPF 的解释很好,猜想微软对 ASP.NET 也有同样的想法。 有关 WPF 绑定选项的说明,请参阅下面的链接。

字段不是绑定源规范的一部分: http://msdn.microsoft.com/en-us/library/ms743643.aspx

【讨论】:

    猜你喜欢
    • 2011-02-10
    • 2010-10-05
    • 1970-01-01
    • 1970-01-01
    • 2011-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-27
    相关资源
    最近更新 更多