GridView找不到定义类型的字段,但找不到匿名类型?

发布于 2024-10-29 19:02:00 字数 815 浏览 0 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少年亿悲伤 2024-11-05 19:02:00

您是否尝试过将它们变成属性而不是字段?某些形式的绑定仅适用于属性并忽略字段。目前尚不清楚您使用的是哪个 GridView,但这可能是问题所在。

最简单的方法是使用自动属性,如下所示

public class DetailComplaint
{
    public decimal Id {get; set; }
    public decimal Status {get; set; }
    public string Name {get; set; }
    public decimal ServiceId {get; set; }
    public string Service {get; set; }
    public string Title {get; set; }
    public string Customer {get; set; }
    public string Description {get; set; }
    public DateTime CreatedDate {get; set; }
    public decimal CreatedBy {get; set; }
    public string Author {get; set; }
    public decimal? AssignedBy {get; set; }
    public decimal? AssignedTo {get; set; }
    public string Technician {get; set; }
    public DateTime? AssignedDate {get; set; }
    public string Contact {get; set; }
}

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

public class DetailComplaint
{
    public decimal Id {get; set; }
    public decimal Status {get; set; }
    public string Name {get; set; }
    public decimal ServiceId {get; set; }
    public string Service {get; set; }
    public string Title {get; set; }
    public string Customer {get; set; }
    public string Description {get; set; }
    public DateTime CreatedDate {get; set; }
    public decimal CreatedBy {get; set; }
    public string Author {get; set; }
    public decimal? AssignedBy {get; set; }
    public decimal? AssignedTo {get; set; }
    public string Technician {get; set; }
    public DateTime? AssignedDate {get; set; }
    public string Contact {get; set; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文