The Data Source '...’ Does Not Support Sorting With IEnumerable Data

We are currently refactoring a web application, which uses ObjectDataSources extensively. There are places in our app, where they are used for sorting grids, but there is a problem. We switched from DataTables to collections and now an exception occurs:

1
2
The data source ‘ods_DataSource’ does not support sorting with IEnumerable data.
Automatic sorting is only supported with DataView, DataTable, and DataSet.

As Pradeem wrote on his blog there are two solutions to the above issue:

  1. Implement custom sorting
  2. Change IEnumerable datatype into one of these datatypes.

Eventually we would implement the former, but for the time being Pradeem gives a solution. There is however something wrong with his code so below I give you his snippet slightly modified to be an Extension method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
public static class Util
{
   public static DataTable ToDataTable<T>(this IEnumerable<T> varlist)
   {
      DataTable dtReturn = new DataTable();
      // column names
      PropertyInfo[] oProps = null;
      // Could add a check to verify that there is an element 0
      foreach (T rec in varlist)
      {
         // Use reflection to get property names, to create table,
         // Only first time, others will follow
         if (oProps == null)
         {
            oProps = ((Type)rec.GetType()).GetProperties();
            foreach (PropertyInfo pi in oProps)
            {
               // Note that we must check a nullable type
               // else method will throw and error
               Type colType = pi.PropertyType;
               if ((colType.IsGenericType) &amp;&amp;
                   (colType.GetGenericTypeDefinition() == typeof(Nullable)))
               {
                  // Since all the elements have same type
                  // you can just take the first element and get type
                  colType = colType.GetGenericArguments()[0];
               }
               dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
            }
         }
         DataRow dr = dtReturn.NewRow();
         // Iterate through each property in PropertyInfo
         foreach (PropertyInfo pi in oProps)
         {
            // Handle null values accordingly
            dr[pi.Name] = pi.GetValue(rec, null) == null
                          ? DBNull.Value
                          : pi.GetValue(rec, null);
         }
         dtReturn.Rows.Add(dr);
      }
      return (dtReturn);
   }
}

So now instead of having a data source method

1
2
3
4
public IList<Model> GetData()
{
   return DAL.GetData();
}

You would have:

1
2
3
4
public DataTable GetData()
{
   return DAL.GetData().ToDataTable();
}

Comments