Bill Blogs in C#

Bill Wagner discusses C#, LINQ, and other items of interest

Creating Data Binding Classes.
Question and Answer on creating and using DataBinding in your own types.

Q: When you bind to business objects, then you not only want to bind
to the properties of the objects, often you want to display properties
of related objects.
 
Example: You have a DataGrid displaying a list of Order objects, the
class Order has a property OrderCustomer that returns a object of class Customer.
When you now display a list of Orders, then whenever you add the
OrderCustomer column to the grid you get a string containing the
name "Cstomer". I guess this happens, because the databinding mechanism
calls object.ToString() on each property. I can think of many instances
where it would be very useful to display the Customer Name in the row
of an order, so this would be a property of a property. 

A:
You are correct.  You get the "Customer" output because of the default ToString behavior. If you override the Object.ToString() method in your Customer object to return the Name property, that behavior would change.

On a case to case basis, this can be achieved if you would define a
property Customer_Name in the Order class. But then there are so many
possibilities, and it does not make sense to code all of these possibilities
when you do create the class. In a certain sense, you then do not follow
the object oriented programming paradigm.
 
I don't think it makes sense to create a list of all properties and sub_properties
that a class has; I believe that there must be a smarter way to do that.
 There are three ways to do this.  Which makes the most sense depends on your application.
 
1: Use the datagrid navigation paradigm.  In this version, you "drilldown" from the Order table into the selected customer(s) in the customer table. This works well when the users will view the navigation up and down the hierarchy using the DataGrid navigation strategy.
 
2: Expose individual sub-properties as you described above. This is the best choice when you have a small number of properties to add.
 
3: Create a new combined data-binding class that contains both the customer and the order objects, and therefore exposes both. This is somewhat of a heavy-weight version of 2.  However, you can use some data-tier logic to create a combined set using a join.



The referenced article.
The original article on DataBinding
Published Wednesday, March 31, 2004 6:33 AM by wwagner
Filed under: ,

Comments

No Comments