Click here to Skip to main content
13,559,022 members
Click here to Skip to main content
Add your own
alternative version


25 bookmarked
Posted 2 Jul 2005
Licenced CPOL

Deep Binding

, 2 Jul 2005
Rate this:
Please Sign up or sign in to vote.
Binding Complex objects to Windows Forms controls


DataBinding is a very powerful tool to push data from objects to the user interface and pull back user data to the object. However, when you have complex objects, Windows Form DataBinding is not so powerful.

In this article, I will demonstrate how we can make a complex databinding between business objects and Windows Forms controls by implementing a few interfaces.

Sample screenshot

The "Complex Object" Sample

The sample here is a book that has Name, PagesNumber and Author properties. The author has name and BirthDate properties. In the object model, we create a book class that implements book properties and a second class Author. The Author property in the Book class is typed as Author.

Here is the code needed in the Book class:

public class Book : BindableObject
public Book()
public Book(string name,int pagesnumber, Author a)
this.Name = name;
this.PagesNumber = pagesnumber;
this.Author = a;
private string _Name;
public string Name
get{return _Name;}
set{_Name = value;}
private int _PagesNumber;
public int PagesNumber
get{return _PagesNumber;}
set{_PagesNumber = value;}
public Author _Author = new Author();
public Author Author 
get{return _Author;}
set{_Author = value;}

The DataBinding Problem

When we want to create a collection of books and show it to the user in datagrid, we can show only the name and Pagesnumber information. That is because in the MappingName property of the DatagridTextBox, we cannot put Author.Name for example to display the name of the book's author.

The Solution

When binding an object to a Windows Form control, the Framework uses reflection to get bindable properties of the object. However, when an object implements the IcustomTypeDescriptor interface, the framework gets object properties from IcustomTypeDescriptor.GetProperties() method. So the solution is to include all the properties we need to display.

In book example IcustomTypeDescriptor.GetProperties() will return : Name, PagesNumber, Author.Name and Author.BirthDate properties.

Doing this, you can display the Author name of a book in a datagrid just by writing "Author.Name" in the MappingName property of the DatagridTextBox. Also if you have a multicolumn combobox, you can display both Book Name and Author Name.

The Source Code

A class name BindableObject is included in the project. This class implements IcustomTypeDescriptor interface. All business objects created (Book and Author classes) inherit from this class. So no code must be written in business classes.

To get properties of an object, reflection is used. To increase performance, I implemented a cache in which I store objects structure, so reflection is used only one time for each type.

When binding a collection, it must implement the ItypedList interface to call the IcustomTypeDescriptor.GetProperties() method. So the BookCollection class inherits from BindableCollection class included in the project.


  • 2nd July, 2005: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Hayder Marzouk
Web Developer
France France
MCSD Asp.Net certified developer

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
manoj kumar choubey8-Aug-12 23:39
membermanoj kumar choubey8-Aug-12 23:39 
QuestionIf instead of having an object Author is a list object of Author? Pin
Member 83045903-Mar-12 5:21
memberMember 83045903-Mar-12 5:21 
Hi Hayder,

If instead of having an object Author inside the object Book, We have a list objects of Author. How would you do?.


Albert Gassó
Computer Software Engineer
QuestionStill the way to go in .NET 2.0? Pin
SoTTo4-Oct-07 23:51
memberSoTTo4-Oct-07 23:51 
AnswerRe: Still the way to go in .NET 2.0? Pin
Hayder Marzouk20-Nov-07 6:25
memberHayder Marzouk20-Nov-07 6:25 
GeneralRe: Still the way to go in .NET 2.0? Pin
Etreus27-Nov-07 23:36
memberEtreus27-Nov-07 23:36 
GeneralBug Fix Pin
shumakercs16-Aug-07 5:14
membershumakercs16-Aug-07 5:14 
GeneralRe: Bug Fix Pin
Hayder Marzouk21-Aug-07 6:11
memberHayder Marzouk21-Aug-07 6:11 
GeneralBindingSource Problem Pin
Primadi12-Jul-06 16:48
memberPrimadi12-Jul-06 16:48 
GeneralRe: BindingSource Problem Pin
Laurts2-Jan-07 2:52
memberLaurts2-Jan-07 2:52 
GeneralRe: BindingSource Problem Pin
Johann de Swardt25-Mar-07 4:28
memberJohann de Swardt25-Mar-07 4:28 
GeneralConversion Pin
stevenjnielsen11-Apr-06 6:45
memberstevenjnielsen11-Apr-06 6:45 
GeneralThis does not work with Iterator properteis Pin
mdn6667-Mar-06 22:53
membermdn6667-Mar-06 22:53 
GeneralLooks like I gotta first bug. Pin
Alexei Tarnakin17-Jul-05 10:45
memberAlexei Tarnakin17-Jul-05 10:45 
GeneralRe: Looks like I gotta first bug. Pin
Anonymous17-Jul-05 23:12
sussAnonymous17-Jul-05 23:12 
General[Message Deleted] Pin
Alexei Tarnakin18-Jul-05 10:50
memberAlexei Tarnakin18-Jul-05 10:50 
GeneralRe: Looks like I gotta first bug. Pin
Alexei Tarnakin19-Jul-05 5:21
memberAlexei Tarnakin19-Jul-05 5:21 
Generalnice job! Pin
Alexei Tarnakin17-Jul-05 10:26
memberAlexei Tarnakin17-Jul-05 10:26 
GeneralRe: nice job! Pin
Anonymous17-Jul-05 23:07
sussAnonymous17-Jul-05 23:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01-2016 | 2.8.180515.1 | Last Updated 2 Jul 2005
Article Copyright 2005 by Hayder Marzouk
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid