locked
Default sort order in ObjectDataSource RRS feed

  • Question

  • User-1460862771 posted
    I have a GridView bound to an ObjectDataSource using my custom methods for retrieving data, paging, sorting, etc, but I would like to specify the default sort order for the first time the page is viewed.

    I tried this by setting the SortExpression in the ODS_Selecting event like this:

    if (!IsPostBack)
    {
        e.Arguments.SortExpression = "SortColumn";
    }

    But that throws a seemingly unrelated error: 
    "The data source 'ObjectDataSource1' does not support sorting with IEnumerable data."

    (I am returning an array of a custom class to my grid, but everything works great if I don't have the Selecting event with these 3 lines of code.)

    The reason I want to specify the sort order is so I can put a sort indicator on the column that is initially sorted by.  As I have it now, there is no sort indicator (because the SortExpression is "") and, even though I could set the default sort order in my select method, the user would have to click the column twice to get the descending sort.
    First visit - SortExpression = ""
    Click the column - SortExpression = "ColumnName"
    Click the column again - SortExpression = "ColumnName DESC"

    I would like to set the SortExpression on the first visit to the page.

    Another related question - Why does the Selecting event fire twice?
    Friday, February 3, 2006 12:32 PM

All replies

  • User-1237437705 posted

    I'm researching the same problem.  If you are using Ilists (hence IEnumerable) it does not support sorting.  See this article here http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnadvnet/html/vbnet01272004.asp

    There are ways around as shown there, but it's not as easy as using a DataSet (from what I can find).  A default GridView (2.0) binding to an ODS using DataSet will allow sorting in the GV.  Using ILists/IEnumerable (like my ODS in the DotNetNuke framework for Data access) will not support this. 

    If anyone has some better links or examples, I'm sure this will become a popular item.  It seemed popular for .Net 1.x DataGrids a while ago.  That changed in 2.0 with the GV for SQL Data Sources, and ironically I'm struggling with this again by using the ODS...

    Tuesday, February 21, 2006 11:13 AM
  • User-479100024 posted

    Just set the following in your Page_Load event:  

    if (!IsPostBack)
    {
        myGridView.Sort("ColumnName", SortDirection.Descending);
    }

    That should assign your sort expression to the objectdatasource's sort parameter the first time the page loads.

    The Selecting event of the objectdatasource fires twice for 1) the SelectMethod and 2) the SelectCountMethod.

    Hope that helps.

    Sunday, October 28, 2007 2:34 AM
  • User1438261371 posted

    That solved my issue, Thanks !

    Tuesday, June 18, 2013 7:59 AM