将对象绑定到 Gridview

发布于 2024-09-12 20:18:07 字数 671 浏览 2 评论 0原文

我有三个类(域、角色和用户)。 Domain 和 Role 是用户类的属性,如下所示:

    public int UserID { get; set; }
    public string UserName { get; set; }
    public Domain Domain { get; set; }
    public Role Role { get; set; }
    public bool Active { get; set; }

Domain 和 Role 只有一个 ID 属性和一个 Name 属性。

我想将用户列表绑定到我的网格视图,但显示域名。目前,我的绑定字段如下所示:

<asp:BoundField DataField="Domain.DomainName" SortExpression="UserDomain" ReadOnly="True" HeaderText="User Domain" />

但这不起作用,出现以下错误:DataBinding: 'Yasur.Business.Entities.User' does not contains a property with name 'DomainName'.

最好的方法是什么?

佩德罗·杜索

I have three classes (domain, role and user). Domain and role are properties of the user class, like this:

    public int UserID { get; set; }
    public string UserName { get; set; }
    public Domain Domain { get; set; }
    public Role Role { get; set; }
    public bool Active { get; set; }

Domain and Role just have a ID property and a Name property.

I would like to bind a user list to my grid view, but showing the DomainName. Currently, I have my bound fields like this:

<asp:BoundField DataField="Domain.DomainName" SortExpression="UserDomain" ReadOnly="True" HeaderText="User Domain" />

But this isnt working, giving the fallowing error: DataBinding: 'Yasur.Business.Entities.User' does not contain a property with the name 'DomainName'.

What is the best way to do this?

Pedro Dusso

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

笑饮青盏花 2024-09-19 20:18:07

我认为您无法对这样的对象进行双向数据绑定。如果您使用具有如下语法的 TemplateField(未测试),则可以“评估”数据:

<asp:TemplateField>
<ItemTemplate>
    <asp:Label ID="Label1" runat="server" Text='<%# ((Domain)Eval("Domain")).DomainName %>' />
</ItemTemplate>

I don't think you'll be able to do two-way databinding with an object like this. You can "Eval" the data if you use a TemplateField with syntax like this (not tested):

<asp:TemplateField>
<ItemTemplate>
    <asp:Label ID="Label1" runat="server" Text='<%# ((Domain)Eval("Domain")).DomainName %>' />
</ItemTemplate>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文