ObjectField - A GridView field

The version of the ObjectField that this post refers to is now out of date. Please go to the ObjectField 1.1 post for the latest version.

I encountered a problem while binding a complex object to a GridView, in that the BoundField doesn’t support specifying a nested value in it’s DataField property. So if you have a list of Customer’s, and want to display the TelephoneNumber property from inside the customer’s ContactDetails property, you’re out of luck.

<asp:BoundField DataField="ContactDetails.TelephoneNumber" />

The above would fall over with an exception along the lines of:
A field or property with the name 'ContactDetails.TelephoneNumber' was not found on the selected data source.

This is a mind-boggling flaw in the BoundField, with the main solution being to create a nested GridView, which is just overkill for most situations. This problem especially rears it’s ugly head if you’re using an ORM layer such as NHibernate or SubSonic.

So what have I done? I’ve just gone and created a solution to this problem.

Introducing the ObjectField, a GridView field that allows binding against hierarchical structured objects. In short, it takes a BoundField and splits it on full-stops (periods!) using each element to find an object.

<jag:ObjectField DataField="ContactDetails.TelephoneNumber" />

The above is now possible! Huzzah.

Extras

There’s one extra thing you should know about. The field has a couple of additional properties that can be useful.

The first is AllowNulls which defaults to true, this will make the field return silently when a null is encountered anywhere along the object hierarchy; this can be useful if you know that there might be a null somewhere along the lines.

The second property is NullValue, which is displayed by the field when AllowNulls is true and a null is encountered. Setting this value allows you to give a user-friendly message if a value is null.

Downloads

The ObjectField is open-source under the new BSD License; read the license for what you’re allowed to do.

The version of the ObjectField that this post refers to is now out of date. Please go to the ObjectField 1.1 post for the latest version.

Comments 3

  1. Sergio wrote:

    Hi!. You are a lifesaver. This class is a gift for me as I use Nhibernate too. Just a question. When I enter into the column designer of the GridView control, it re-writes the markup so I loose the custom markup (

    Posted 24 Apr 2008 at 3:26 pm
  2. James Gregory wrote:

    Glad it was helpful. It’s currently completely un-designer friendly, as you’ve noticed. I presume it needs some of the designer classes setting up, I’ll take a look at the code next time I get a minute.

    Posted 24 Apr 2008 at 3:32 pm
  3. Sergio wrote:

    I’m totally new to designers. I’d like to know where to start on this topic.

    Posted 25 Apr 2008 at 7:48 am

Trackbacks & Pingbacks 1

  1. From Data-binding hierarchical objects — James Gregory on 21 Feb 2008 at 12:16 pm

    [...] ← ObjectField - A GridView field [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *