views:

203

answers:

1

Hello, 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.ComponentModel.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 ?

+1  A: 

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;
    }
}
Fredrik Mörk
I works, thank you.So DefaultBindingProperty has nothing to do with this, so what is DefaultBindingProperty purpose?
Pr0fess0rX
My interpretation is that the intended use of `DefaultBindingProperty` is exactly this, but that the control for some reason do not use the information.
Fredrik Mörk
thank you Fredrik :)
Pr0fess0rX