将对象绑定到 Gridview
我有三个类(域、角色和用户)。 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您无法对这样的对象进行双向数据绑定。如果您使用具有如下语法的 TemplateField(未测试),则可以“评估”数据:
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):