I am working on a winforms application where I am rendering domain/object data via an ultrawingrid. I am using a bindingsource to bind the object to the grid.For simple objects this works quite well.
The thing I'm trying to get my head around is rendering an object with nested objects for e.g a Person class will have a property of Address class. I would like to display the properties of Address (Street, City, Country) as columns on the grid along with the properties of the Person class.
The grid has to be editable and any user changes need to reflect back on the domain object (which I'm doing via the binding source).
What's the best way to go about this?
Binding
I typically use some sort of code like this:
The binding list will handle the add/remove of rows for you, but it doesn't know about the properties inside
Person
. To get that part of the binding to work, you'll need to havePerson
implement INotifyPropertyChanged. This will notify the ultragrid when the properties have changed. The code will look something like this (yes, unfortunately this makes it so you can't use auto-implemented properties):Flattening object hierarchies
It looks like what you're ask for isn't directly possible. There are a few options:
Person
class to expose the properties ofAddress
with some pass through code to handle the setting of the properties.(I can provide a code samples if needed)
One-to-many Nested Objects
If you, for example, had multiple addresses per person, you could show them nested in an expandable section under each
Person
row. To do this, inside yourPerson
you'd have aBindingList(Of Address)
which also implementsINotifyPropertyChanged
. Not exactly what you want, but an option :)Words of caution
A few notes if you are doing MVP. You'll need to have the same reference to the
BindingList
in your view and presenter, obviously. Also, if you need to reset the contents, I'd recommend callinglist.Clear()
instead of creating a new one. If you create a new one in your presenter, you'll have broken the connection with theUltraGrid
and you'll have to re-set theDataSource
property in the view.