GridView找不到定义类型的字段,但找不到匿名类型?
我使用 LINQ 从数据库查询数据,然后将其绑定到 GridView。当我使用匿名类型时,一切都很好,但现在我想重用数据(用于以后的过滤器),所以我创建了一个类来映射匿名类型,这里是:
public class DetailComplaint
{
public decimal Id;
public decimal Status;
public string Name;
public decimal ServiceId;
public string Service;
public string Title;
public string Customer;
public string Description;
public DateTime CreatedDate;
public decimal CreatedBy;
public string Author;
public decimal? AssignedBy;
public decimal? AssignedTo;
public string Technician;
public DateTime? AssignedDate;
public string Contact;
}
然后在查询中使用它(... select new DetailComplaint { //all fields are as same as before })
但是现在当我绑定数据时,GridView 会抛出异常,它找不到名为“Id”的字段或属性,而该字段或属性是在 DetailComplaint 中显式定义的。
我该如何解决这个问题?谢谢
I'm using LINQ to query data from database and then bind it to a GridView. When I use anonymous type, everything is OK, but now I want to reuse the data (for later filter), so I created a class to map the anonymous type, here it is:
public class DetailComplaint
{
public decimal Id;
public decimal Status;
public string Name;
public decimal ServiceId;
public string Service;
public string Title;
public string Customer;
public string Description;
public DateTime CreatedDate;
public decimal CreatedBy;
public string Author;
public decimal? AssignedBy;
public decimal? AssignedTo;
public string Technician;
public DateTime? AssignedDate;
public string Contact;
}
and then use it in the query (...select new DetailComplaint { //all fields are as same as before })
but now when I bind data, the GridView throws an exception that it can't find a field or property named "Id", which is explicitly defined in DetailComplaint.
How can I fix this? Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过将它们变成属性而不是字段?某些形式的绑定仅适用于属性并忽略字段。目前尚不清楚您使用的是哪个 GridView,但这可能是问题所在。
最简单的方法是使用自动属性,如下所示
Have you tried making these into properties instead of fields? Some forms of binding will only work with properties and ignore fields. It's unclear which
GridView
you are using but this is possibly the problem.The easiest way to do that here is to use auto-properties like so