How to set SortExpression of GridView before calling DataBind()

Posted on 2009-04-20
Last Modified: 2012-05-06
Hi there

I have a GridView whose columns are created dynamically.  I have an ObjectDataSource too but my page allows records to either be cached or to be requested from the database with each post back therefore I also set the dataSource of my grid dynamically to either my cached DataTable or to the Select method results from my DataSource.

Therefore when I want to sort by one of my dynamically created columns I provide a gridView_Sorting method and in there I chek whether i am caching or not if I am I just sort my cached DataTable as follows:

dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

in my GridView_Sorting method.

How ever if I am not caching then I bind my GridView to my ObjectDataStore and then call DataBind on the GridView however now my GridView.SortEXpression is not set.  I know my column sort expression because it's in e.SortExpression but this doesn't get passed to my ObjectDataStore's select method so how do I pass it.

My code is:

MyGridView.DataSource = MyDataStore;

so how to ste MyGridView.SortExpression to e.SortExpression??

Many thanks
Question by:Samm1502
    LVL 14

    Accepted Solution


    You can directly set

    MyGridView.Sort="<column name";
    MyGridView.SortDirection = SortDirection.Ascending;

    Author Closing Comment


    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticallā€¦
    In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Siā€¦
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now