获得类属性绑定值,DefaultBindingProperty工作不适合我! [英] Get binded value from class property, DefaultBindingProperty is not working for me!

查看:101
本文介绍了获得类属性绑定值,DefaultBindingProperty工作不适合我!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了我的项目的一些实体(类),我想设置默认为它暴食性,下面是一个例子。

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; }
    }
}

如果我有列表<家庭> ,并希望将其绑定到一个GridView和添加了这个字段< ASP:BoundField的数据字段=父亲/> ,结果将是 MyNamespace.Person ,但我需要它来填充属性名称的值不使用模板列所以我错过了什么?或 DefaultBindingProperty 是不正确的属性?

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 ?

推荐答案

的问题是财产父亲是类型。有一个人没有明显的弦重新presentation 值,因此显示类型的名称。尝试重写的ToString 方法来显示你想要什么:

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;
    }
}

这篇关于获得类属性绑定值,DefaultBindingProperty工作不适合我!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆