【问题标题】:Bind child Properties to DataGridView将子属性绑定到 DataGridView
【发布时间】:2009-01-13 02:20:21
【问题描述】:

有没有办法将对象的子属性绑定到 datagridview?这是我的代码:

public class Person
{
    private string id;
    private string name;
    private Address homeAddr;
    public string ID
    {
        get { return id; }
        set { id = value; }
    }
    public string Name
    {
        get { return name; }
        set { name = value; }
    }
    public Address HomeAddr
    {
        get { return homeAddr; }
        set { homeAddr = value; }
    }
}

public class Address
{
    private string cityname;
    private string postcode;
    public string CityName
    {
        get { return cityname; }
        set { cityname = value; }
    }
    public string PostCode
    {
        get { return postcode; }
        set { postcode = value; }
    }
}

当 Person 类型的对象绑定到 datagridview 时,我想显示 ID、Name、CityName。请注意,CityName 是 HomeAddr 的属性。

【问题讨论】:

  • 您希望 CityName 和 PostCode 放在一列还是两列?

标签: c# data-binding datagridview


【解决方案1】:

如果您将 DataGridView 设置为 AutoGenerateColumns = true,那么确实没有简单的方法可以做到这一点。最好的办法是提前设置 DataGridView,然后手动填充 DataGridView。

或者,您可以实现ITypedList,但如果您问我,那会有点痛苦。

【讨论】:

    【解决方案2】:

    嗯,I've found a way to do this, in FarPoint。

    但是如果您的对象没有列表类型属性,您可以在 DataGridView 中执行此操作。

    【讨论】:

      【解决方案3】:
      【解决方案4】:

      BLToolkit 有 BLToolkit.ComponentModel.ObjectBinder

      这些功能是:

      Support for field binding along with property binding.
      
      Support for inner class field and property binding such as Order.Address.Line1.
      
      Support for the ObjectView feature which is available by assigning an object view type to the ObjectBinder.ObjectViewType property. An object view is an object that implements the IObjectView interface. This interface includes only one property - object Object { get; set; }. An object view can implement additional properties based on the assosiated object. The ObjectBinder will combine all of these properties with main object properties and create a single PropertyDescriptor collection. This feature can be used to separate UI presentation logic from business model objects and to keep them clean. ObjectView should be a stateless, lightweight object as its single instance can be assigned to many assosiated objects.
      
      The ObjectBinder is optimized for high performance applications such real-time multithreaded message processing and distribution banking systems. So it does not use reflection to access objects. The standard way (which is used by the BindingSource) is to call the TypeDescriptor.GetProperties method to get a PropertyDescriptor collection. This method creates property descriptors that access object properties by reflection. The ObjectBinder has its own mechanism to avoid unnessasy reflection and boxing/unboxing operations.
      

      【讨论】:

        猜你喜欢
        • 2023-01-12
        • 2016-04-26
        • 2012-10-15
        • 1970-01-01
        • 2018-07-24
        • 2013-12-19
        • 1970-01-01
        • 2011-08-26
        • 2010-10-28
        相关资源
        最近更新 更多