How to Set BoundField.DataField to a Nested Property
Very often, we need to bind a property of a nested object in a GridView column. Suppose you have OrderItem class that has a reference to Order class. Imagine you have a List of OrderItems that is bound to a GridView and you wanted to display a property from the Order class. ASP.NET GridView (or to be precise bounded fields) does not support this. A familiar situation? We will fix this soon. Let's create a little class and call it NestedPropertyBoundedField. We will use Reflection and BoundField as the basis (you could also use DataControlField as a super class, but let's stick to BoundField).
I recently needed this type of function in my GridViews. Mostly I use GridViews from code, so this class needs some more bells and whistles to be design-time friendly or if you want to use declaratively. However, the core functionality is the same. Without much ado, the source code is given below:
This is the beauty of OOP and inheritance in particular. The possibilities are endless. For example, I also use a whole array of other data bound fields for my GridViews. In of of the upcoming posts I will write about a composite data bound field which is really cool.
I recently needed this type of function in my GridViews. Mostly I use GridViews from code, so this class needs some more bells and whistles to be design-time friendly or if you want to use declaratively. However, the core functionality is the same. Without much ado, the source code is given below:
public class NestedPropertyBoundedField : BoundField
{
protected override string FormatDataValue(object dataValue, bool encode)
{
return base.FormatDataValue(dataValue, encode);
}
protected override object GetValue(Control controlContainer)
{
string[] pathItems = DataField.Split('.');
object currentDataObject = null;
if(controlContainer is IDataItemContainer)
{
currentDataObject = (controlContainer as IDataItemContainer).DataItem;
foreach (string item in pathItems)
{
PropertyInfo[] propertyInfos = currentDataObject.GetType().GetProperties();
PropertyInfo propertyInfo = propertyInfos.FirstOrDefault(i => i.Name == item);
if (propertyInfo == null)
{
throw new System.Web.HttpException("A field or property with the name '"+item+"' was not found on the selected data source.");
}
currentDataObject = propertyInfo.GetValue(currentDataObject, null);
}
return currentDataObject;
}
return null;
}
}
This is the beauty of OOP and inheritance in particular. The possibilities are endless. For example, I also use a whole array of other data bound fields for my GridViews. In of of the upcoming posts I will write about a composite data bound field which is really cool.



Visual Studio has a nice feature to add existing files to a project and the process if simple and straightforward. What about adding an existing folder to a project? This is especially useful if you accidentally remove a folder from a project. A typical action done by developers in this case is to create a new folder in the project, add existing files there, delete the existing folder and then rename the new folder.