Geeks With Blogs
CPound and a Cup of Coffee CPoundAndACupOfCoffee blog = new CPoundAndACupOfCoffee(thoughts);

GridView! YES! (...1 hour passes...) Dah! GridView! *Fist Shaking*

Coming from a Java background I have to say that the standard toolset that Microsoft provides in .NET 3.5 is impressive by any standard.  Perhaps the fact that it is standard is what makes it so powerful.... That being said I want to just give a quick rundown illustrating the rollercoaster ride that was my first run in with GridView from .NET 2.0. 

I first wanted to just populate a table, after a quick googling I landed here:

Looks pretty impressive after reading that article and toying around with the features that it presented I was even more impressed.  Everything was so simple and easy an involved almost no coding.  So after getting comfortable with the way that the GridView works it was on to actually using it in my project…

I am using Linq as my ORM (which is a whole other post in itself) and I have a bunch of complex objects, for simplicity sake we will just look at two.  A MemberInfo object and an address.  Address is a nested property of MemberInfo.  So… I have a multi tier architecture and I use my business logic layer as the datasource  for my GridView and it auto generates my columns for me and it looks like this:

Id | firstName | lastName | addressId | ……

Pretty slick.  Now on to getting some information from the address that isn’t completely useless for the user. I drop to the source view and use the standard java notation for accessing nested properties. 


One would imagine that it would be that easy—but no.  Why would it be?  Apparently Mircosoft with it’s infinite resources can’t do what a little free Java library like DisplayTag and a little jstl can do. 

Back to google again, I land here:

Hmmm… now I’m upset, I was hoping that Microsoft just had some sweet new way of binding to complex objects but this blog is saying I actually have to cast objects and not only that but INSIDE my presentation file.  That leaves a bad taste in my mouth to be sure.  There MUST be something else, but at least now I have a last resort.  Google again, I land here this time:

I figure that there HAS to be some sort of example in the Microsoft GridView example.  I figured that the templateField was going to be the way to go so I opened TemplateField -> embeddedControl example. 

I changed my GridView code to add the attribute OnRowDataBound="GridViewId_RowDataBound" then changed my column to this:

<asp:TemplateField HeaderText="Street">
        <asp:Label ID="StreetLabel" runat="server" Text="Street"></asp:Label>


Then in my code behind page I just had to drop this function:

protected void GridViewId_RowDataBound(object sender, GridViewRowEventArgs e)
    if (e.Row.RowType == DataControlRowType.DataRow)
            Label l = (Label)e.Row.FindControl("StreetLabel");
            MemberInfo info = (MemberInfo)e.Row.DataItem;
            l.Text = info.Address.Street;


Okay, so it still involves a cast but I think it is the cleanest way that I have found.  Kinda weak, and it is unfortunate since most of my GridViews are involve displaying complex objects. If anyone has any better way to do this please feel free to post it. 

I also enjoyed this post along the same lines: Posted on Sunday, January 13, 2008 11:16 AM | Back to top

Comments on this post: .Net GridView and Complex Objects

# re: .Net GridView and Complex Objects
Requesting Gravatar...

First of all, I must say: I'm not a .NET expert. I also come from Java world.

I'm facing the same issue but when WRITING objects to the database. I have been struggling with complex objects and still I have not found a smooth solution (not one like JSF syntax "#{}")...

However, I think I have some clues but nothing sure...

1. Hack properties("AddressID")for PK's only, for example:
"DTOPerson":(id, name, DTOAddress, *AddressID*)
"DTOAddress":(id, country, state, street)

I've found it useful for 2-way data-binding syntax(WRITE). That way I avoid manually searching the control and extracting its value. I delegate that automatic extracting to DataSources: SQLDataSource or ObjectDataSource(for n-tier architectures).

2. A similar approach you use: handling events, searching manually the control and extracting its value. I really don't like's prone to developer's mistakes.

3. Change my object's design to work smoothly with DataBound controls: GridView, FormView, etc. No way! I believe firmly in OOP principles.

4. Design the way user interacts with the views (ASPX) in order to populate a complex object. Maybe 2 FormsView controls: 1 for DTOPerson and 1 for DTOAddress?

5. Nested data-bound controls?

6. Handling different ObjectDataSources?

By the way, I think in your post you're doing a read operation. In that case you can use 1-way data-binding syntax: {%# Eval("Street.Name")} whick works great...but not the same when using a 'write' scenario(2-way data-binding) {%# Bind("Street.Name")} DOES NOT compile although both expressions use the same grammar! That's interesting...

I hope we can find a nice solution to this...I think it's a widespread issue among serious n-tier developers. Maybe with ASP.NET MVC this can be solved...not sure anyway.

Left by Julio César on Apr 15, 2008 7:01 PM

# re: .Net GridView and Complex Objects
Requesting Gravatar...
Left by adsf on Jul 09, 2008 1:58 AM

# re: .Net GridView and Complex Objects
Requesting Gravatar...
I had this problem today in one of my projects.

Thank you for this simple solution!

Left by Alex on Oct 03, 2008 6:35 AM

# re: .Net GridView and Complex Objects
Requesting Gravatar...
The easiest way to do this is to extend the BoundField and provide your own implementation for GetValue For Example:

public class EnhancedBoundField : BoundField


protected override object GetValue(Control controlContainer)


object component = null;

string dataField = this.DataField;

if (controlContainer == null)


throw new HttpException("DataControlField_NoContainer");


component = DataBinder.GetDataItem(controlContainer);

if ((component == null) && !base.DesignMode)


throw new HttpException("DataItem_Not_Found");


// use reflection to get the data from your child...


Left by Ovidiu Petruescu on Aug 14, 2009 11:18 PM

# re: .Net GridView and Complex Objects
Requesting Gravatar...
you can use this syntax e.g i want to bind with property myproperty in mybuisnessObj.InnerObject

<%# DataBinder.Eval(DataBinder.Eval(Container.DataItem, "InnerObject"), "myproperty ")%>

Make sure to use asp:TemplateField when using this synatx
Left by Amna Irshad on Aug 19, 2009 12:17 AM

Your comment:
 (will show your gravatar)

Copyright © Joe Aukeman | Powered by: