Привязать дочерние свойства к DataGridView - PullRequest
2 голосов
/ 13 января 2009

Есть ли способ связать дочерние свойства объекта с сеткой данных? Вот мой код:

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

И я хочу показать ID, Name, CityName, когда объект типа Person связан с сеткой данных. Обратите внимание, что CityName является собственностью HomeAddr.

Ответы [ 4 ]

0 голосов
/ 15 октября 2013

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.
0 голосов
/ 13 января 2009

Хм, Я нашел способ сделать это в FarPoint .

Но вы можете сделать это в DataGridView, если у вашего объекта нет свойства типа списка.

0 голосов
/ 09 июля 2009

Вы также можете посмотреть здесь

http://www.mail-archive.com/advanced-dotnet@discuss.develop.com/msg06383.html

0 голосов
/ 13 января 2009

Если у вас есть DataGridView для AutoGenerateColumns = true, то на самом деле простого способа сделать это не существует. Лучше всего заранее настроить DataGridView и вручную заполнить DataGridView.

В качестве альтернативы, вы можете реализовать ITypedList , но это немного больно, если вы спросите меня

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...