Binding to subproperties

Posted by: c1_bernardoc on 27 September 2017, 7:05 pm EST

    • Post Options:
    • Link

    Posted 27 September 2017, 7:05 pm EST

    I was recently asked about binding to objects that contain other objects as properties, and whether there was a way to expose those sub-properties as columns on the grid. At the time, I suggested using OwnerDraw to display the value of the sub-properties in the grid cells, using the column't UserData property to identify which sub-property to bind to. The solution worked but was far from perfect, since it did not support editing or sorting.

    I did some research and found a much better solution, which I think might be useful to some of you.

    The solution is based on a custom data source class that extends the standard BindingList to provide sorting and deep property binding. Basically, the DeepBindingList class implements the ITypedList interface and exposes not only the properties of T but also all the sub-properties. For example, if you had the following classes:

    public class Customer { public int ID { get; set; } public string Name { get; set; } public Address Address { get; set; } } public class Address { public string Street { get; set; } public string City { get; set; } public string Country { get; set; } }

    Creating a BindingList and using it as a data source for a grid, you would get three columns: ID, Name, and Address. This last column would not be very useful at all unless you implemented custom TypeConverters, UIEditors, and the like (such as OwnerDraw columns ;-).

    But if you created a DeepBindingList, you would get all the inner properties as well: ID, Name, Address, Address.Street, Address.City, and Address.Country. All the columns would be editable and sortable as usual.

    The sample attached shows how the DeepBindingList works. I hope you find it useful.

    2011/01/243814_DeepBindingList.zip
  • Posted 10 October 2017, 12:36 am EST

    Do you have a vb.net example of the deep binding list class.

  • Posted 12 October 2017, 12:32 am EST

    Hi!

    Here is the equivalent sample in VB for you.

    Regards,

    Meenakshi

    Prj_DeepBindingList_VB.zip

  • Posted 14 October 2017, 3:54 am EST

    Thank you so much… very helpful.

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels