Wednesday, January 28, 2009

How to bind a List with custom objects to a Gridview using an ObjectDataSource

First of all it's important to understand how the ObjectDataSource works.

Here is a quote from MSDN..

The ObjectDataSource is an ASP.NET data source control that represents a data-aware middle-tier object or a data interface object to data-bound controls. You can use the ObjectDataSource control in conjunction with a data-bound control to display, edit, and sort data on a Web page with little or no code.

A very common application design practice is to separate the presentation layer from business logic and to encapsulate the business logic in business objects. These business objects form a distinct layer between the presentation layer and the data tier, resulting in a three-tier application architecture. The ObjectDataSource control enables developers to use an ASP.NET data source control while retaining their three-tier application architecture.

The ObjectDataSource control uses reflection to create instances of business objects and to call methods on them to retrieve, update, insert, and delete data. The TypeName property identifies the name of the class that the ObjectDataSource works with. The ObjectDataSource control creates and destroys an instance of the class for each method call; it does not hold the object in memory for the lifetime of the Web request. This is a serious consideration if the business object that you use requires many resources or is otherwise expensive to create and destroy. Using an expensive object might not be an optimal design choice, but you can control the life cycle of the object by using the ObjectCreating, ObjectCreated, and ObjectDisposing events.

In this article I'm going to build further on what I did in this post.

Firstly I commented the code in the Page_Load of Default.aspx. After that, I dragged an ObjectDataSource to my Default.aspx using the designer.

I right-click on the ObjectDataSource and choose to configure it, which opens a wizard.

First I choose a Business Object. In this case it will be the Persons Class.

Step 1 of the wizard

After choosing my Business Object, I need to select a method which returns data. This method needs to return a datataype which implements IENumerable!

After selecting my select method, I finish the wizard.

Step 2 of the wizard

Now I configure my GridView by using the smarttag on the GridView. I set the ObjectDataSource of the GridView and enable Paging.

Configuring the GridView

And voila, as easy as that.


I really hope Inserting, Deleting and Updating data is going to be as easy as this!

I think this is a great way to build a robust application where you have full control of what you are doing. You can debug right into your Business Objects. The only downside is that you have to write a little more code, but this does not outweigh the advantage of full control.


  1. Mercikes Jef! Ge hebt dat goed gedaan! ;-)

  2. Geen probleem Kris, voor u doenekik bak.