GridView Sorting Trick when using Object Datasource with custom objects
When you are using Object DataSource in ASP.Net for GridView, if your select method is returning an object that is of type DataView, DataTable or DataSet, you will be able to get sorting automatically.
But if you are like me and your architecture has a distinct DTO (Entity) layer, and you pass back and forth custom objects or generic list of these custom objects, then you are in less luck with GridView. If you
are binding GridView with Object Datasource that returns a List, you will need to implement your own custom code to do sorting.
If you google this issue, you will find different implementation that will point you to add a OnSorting event handler and more than one way to implement a custom sorting.
What my thinking is, since DataTable and DataView provides us the automatic sorting that we need, then why don’t we create
a DataTable from the List we have. We can do this elegantly using generics and reflection. Once we create a DataTable, simply pass back a DataView based on this table as return parameter from your Select Method of Object Datasource.
Here is the code for converting a List to a DataTable, (CustomObject is my custom object defined in my DTO layer)
using System;
using System.Data;
using System.Collections.Generic;
using System.Reflection;
public class Utilities
{
public static DataTable ToDataTable(List lst) where T : CustomObject
{
PropertyInfo[] propertyInfos;
System.Data.DataTable table = new DataTable("GridViewTable");
DataColumn column=null;
DataRow row=null;
foreach (CustomObject v in lst)
{
propertyInfos = v.GetType().GetProperties();
foreach (PropertyInfo propertyInfo in propertyInfos)
{
column = new DataColumn();
column.DataType = System.Type.GetType(propertyInfo.PropertyType.FullName);
column.ColumnName = propertyInfo.Name;
table.Columns.Add(column);
}
//New table made, break the loop
break;
}
//Now populate the table with values
foreach (CustomObject v in lst)
{
propertyInfos = v.GetType().GetProperties();
row = table.NewRow();
foreach (PropertyInfo propertyInfo in propertyInfos)
row[propertyInfo.Name] = propertyInfo.GetValue(v, null);
table.Rows.Add(row);
}
return (table);
}
}
Cheers
4 comments so far
Leave a reply
Beautiful Binu! By far the cleanest and most abstract approach to this issue that I have seen while searching for solutions… Can’t wait to get it a try…
This is great but really does not get everything I want. My objects have DataObjectField attributes on them so that the gridview can pretty much auto populate. Doesn’t using this method just blow that away?
I would love to see how to create a DataView with attributions so that when the GUI editor picks it up it knows everything about the object.
Any ideas?
[...] Comments Christian Jensen on GridView Sorting Trick when using Object Datasource with custom objectsRobert W. on Internet Explorer 8.0 rollback to 7.0dotnetarchitect on Using LINQ to manipulate [...]
[...] Comments dotnet etc. on GridView Sorting Trick when using Object Datasource with custom objectsChristian Jensen on GridView Sorting Trick when using Object Datasource with [...]