Tech – Noesis

The more you know, you know how little you know

Solution to Error: The data source ‘ods_DataSource’ does not support sorting with IEnumerable data. Automatic sorting is only supported with DataView, DataTable, and DataSet.

Posted by Pradeep Mishra on March 3, 2008

Suppose you have a grid view and associated datasource as ods_DataSource.

  

<!-- Some other code---->


The select method is defined as

public ISingleResult SelectMethod(){
//Get DAL Instance DALInstance
return DALInstance.GetDATA(){}
}
Now if you try to sort on some column the following error pops up.The data source ‘ods_DataSource’ does not support sorting with IEnumerable data. Automatic sorting is only supported with DataView, DataTable, and DataSet.This is because to implement sorting you must have datasource of type DataView/DataTable/DataSet. Now if you are using traditional 3 tier architecture and returning one of these three datatypes everything works fine. The problem arises when you are using Linq. There are two possible solution for this..

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

Here we will try to follow 2nd approach. In our data access layer we will change IEnumerable into a DataTable using an static utility class. Here is the code to perform that operation

public static class Util
{
public static DataTable ToDataTable(this IEnumerable varlist, CreateRowDelegate fn)
{
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) && (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);
}
public delegate object[] CreateRowDelegate(T t);
}
Changes in the Select method

public ISingleResult SelectMethod(){
//Get DAL Instance DALInstance
return DALInstance.GetSomeData(){}
}
public DataTable GetSomeData()
{
ISingle result = //code to get result;
return (result.ToDataTable(rec => new object[] { result}));
}
As you can see ToDataTable is an extenstion method and will be available to all IEnumerable types.Obviously there are some performance overhead due to use of reflection but still this approach can be used. However for real time applications performance testing must be performed.

Hope this helps!

15 Responses to “Solution to Error: The data source ‘ods_DataSource’ does not support sorting with IEnumerable data. Automatic sorting is only supported with DataView, DataTable, and DataSet.”

  1. Peter said


    I am getting an error on the section

    public static DataTable ToDataTable(this IEnumerable varlist, CreateRowDelegate fn)

    Error 4 Using the generic type 'System.Collections.Generic.IEnumerable' requires '1' type arguments C:\Documents and Settings\freeborp\My Documents\Visual Studio\Projects\Internet\ReqSys\ReqSys\ARSDAL\Utility.cs 12 54 ReqSysBiz

  2. Peter said

    figured it out I needed using System.Collections; not using System.Collections.Generic;

  3. Peter said

    OK new question I am getting a compile error on the T in this line

    # public delegate object[] CreateRowDelegate(T t);

    the message is…

    Error 3 The type or namespace name ‘T’ could not be found (are you missing a using directive or an assembly reference?)

  4. youporn said

    hi great site nice work thanks like youporn com =) redtube asian see u

  5. mesut said

    I also get an error see below… Did someone able to solve the problem?
    thanks mesut

    Error 3 The type or namespace name ‘T’ could not be found (are you missing a using directive or an assembly reference?)

    I tried: public ISingleResult SelectMethod(){
    //Get DAL Instance DALInstance
    return GetSalesOrder();
    {}
    }

    and also

    public ISingleResult SelectMethod(){
    //Get DAL Instance DALInstance
    return GetSalesOrder();
    {}
    }

  6. great thanks

  7. College said

    College…

    […]Solution to Error: The data source ‘ods_DataSource’ does not support sorting with IEnumerable data. Automatic sorting is only supported with DataView, DataTable, and DataSet. « Tech – Noesis[…]…

  8. […] Pradeem wrote on his blog there are two solutions to the above […]

  9. backlinks said

    backlinks…

    […]Solution to Error: The data source ‘ods_DataSource’ does not support sorting with IEnumerable data. Automatic sorting is only supported with DataView, DataTable, and DataSet. « Tech – Noesis[…]…

  10. I.T Solutions Toronto said

    Woah! I’m really digging the template/theme of this website. It’s
    simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and appearance. I must say you’ve
    done a very good job with this. Additionally, the blog loads super fast
    for me on Chrome. Superb Blog!

  11. Terri said

    Everything published was very reasonable. But, what
    about this? suppose you typed a catchier title?
    I mean, I don’t want to tell you how to run your blog, however suppose you added something that grabbed people’s attention?
    I mean Solution to Error: The data source ‘ods_DataSource’ does not support sorting with IEnumerable data.
    Automatic sorting is only supported with DataView, DataTable, and DataSet.
    « Tech – Noesis is kinda boring. You could
    look at Yahoo’s home page and watch how they create post titles to get viewers to open the links. You might try adding a video or a pic or two to grab people interested about what you’ve
    written. In my opinion, it might bring your website a little livelier.

  12. I absolutely love your blog.. Excellent colors & theme.
    Did you make this site yourself? Please reply back as I’m wanting to create my own site and would like to know where you got this from or just what the theme is called. Thank you!

  13. Hello mates, how is everything, and what you wish for to say on
    the topic of this article, in my view its in fact amazing in favor of me.

  14. tech blog said

    What’s Taking place i’m new to this, I stumbled upon this I
    have found It absolutely useful and it has aided me out loads.
    I hope to give a contribution & help other customers like
    its aided me. Great job.

  15. 7WLIJH2A www.yandex.ru said

    0olbBUGI

Leave a comment