Get binded value from class property, DefaultBindingProperty is not working for me!
I'm creating some entities (class) for my project and I want to set a default binging property for it, here is an example
namespace MyNamespace
{
[System.ComponentMod开发者_高级运维el.DefaultBindingProperty("Name")]
public class Person
{
public int ID { get; set; }
public string Name { get; set; }
public int Gender { get; set; }
}
public class Family
{
public int ID { get; set; }
public Person Father { get; set; }
}
}
if I have List<Family> and want to bind it to a GridView and added this field <asp:BoundField DataField="Father" /> the result will be MyNamespace.Person but I need it to populate the value of the property Name without using TemplateField so did I miss something? or DefaultBindingProperty is not the right Attribute ?
The problem is the the property Father is of the type Person. There is no obvious string representation of a Person value, so the name of the type is displayed. Try to override the ToString method to show what you want:
public class Person
{
public int ID { get; set; }
public string Name { get; set; }
public int Gender { get; set; }
public override string ToString()
{
return Name;
}
}
加载中,请稍侯......
精彩评论