Click here to Skip to main content
16,021,580 members
Articles / Programming Languages / C#

ASP.NET Controls – Problem sorting GridView with SqlDataSource control

Rate me:
Please Sign up or sign in to vote.
4.33/5 (2 votes)
13 Apr 2010CPOL 14.6K   7   1
ASP.NET Controls – Problem sorting GridView with SqlDataSource control

Let me start by saying that Microsoft does not consider this issue as a problem. As you can see here, this is a “by design” behavior.

The problem is well described in the referred Connect feedback and it contains a workaround.

Although simple, the workaround requires you to always register the GridView Sorting event and make the tweak according to the current GridView settings. Well, if you are like me, you will forget to do it half the times needed.

So, I made a not so simple workaround that will take care of the issue for me.

I override the OnSorting method from GridView so I can handle the GridViewEventArgs instance and override its SortDirection value.

To turn this into a general solution, I partially reproduce the ParseSortString method from DataTable to find out if the current SortExpression contains either the ASC or DESC keywords.

Here is the code:

C#
public class GridView : global::System.Web.UI.WebControls.GridView
{    
    protected override void OnSorting(GridViewSortEventArgs e)
    {
        if (!string.IsNullOrEmpty(this.SortExpression))
        {
            if (this.SortExpression.Equals(this.SortExpression))
            {
                bool isMultipleSortExpression;
                SortDirection? sortDirection = GetSortDirection
		(this.SortExpression, out isMultipleSortExpression);
                if (sortDirection.HasValue)
                {
                    // To undo bug in GridView.HandleSort(string sortExpression) 
                    // and then in GridView.CreateDataSourceSelectArguments()
                    e.SortDirection = SortDirection.Ascending;
                }
            }
        }
        base.OnSorting(e);
    }
    private SortDirection? GetSortDirection
	(string sortExpression, out bool isMultipleSortExpression)
    {
        SortDirection? sortDirection = null;
        isMultipleSortExpression = false;
        string[] strArray = sortExpression.Split(new char[] { ',' });
        for (int i = 0; i < strArray.Length; i++)
        {
            string strA = strArray[i].Trim();
            int length = strA.Length;
            if ((length >= 5) && (string.Compare(strA, length - 4, 
		" ASC", 0, 4, StringComparison.OrdinalIgnoreCase) == 0))
            {
                sortDirection = SortDirection.Ascending;
            }
            else if ((length >= 6) && (string.Compare(strA, length - 5, 
		" DESC", 0, 5, StringComparison.OrdinalIgnoreCase) == 0))
            {
                sortDirection = SortDirection.Descending;
            }
            if (!sortDirection.HasValue)
            {
                break;
            }
        }
        if (sortDirection.HasValue)
        {
            if (strArray.Length > 1)
            {
                isMultipleSortExpression = true;
            }
        }
        return sortDirection;
    }
}

Enjoy it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect everis
Portugal Portugal
Over 13 years of experience in the Software Development working mainly in the banking and insurance industry.

Over 3 year of experience as Operations Team Leader focused on Infrastructure Management and Software Configuration Management.

I've been honored with the Microsoft Most Valuable Professional (MVP) Award for three consecutive years, 2010, 2011 and 2012, in recognition to exceptional technical contributions and leadership.

Current / Recent Technical Projects
- Dominican Republic Instance management, including 2nd line System management, capacity management, SW monitoring and deploy management
- Colombian SECOPII Instance management, including 2nd line System management, capacity management, SW monitoring and deploy management
- Vortal Main Instance management, including 2nd line System management, capacity management, SW monitoring and deploy management
- Vortal Development ecosystem management, including Server management, , capacity management, SW monitoring and deploy management

Areas of Specialization:
- Operations Management - ISO 20000 & ISO 27001 driven
- Team Management and Coaching
- Technology Leadership, Solutions/Architecture
- Product life cycle management, Continuous Integration
- Technological background in Microsoft frameworks and tools.

Comments and Discussions

 
GeneralMy vote of 3 Pin
Gilberto Francisco21-Jul-10 10:16
Gilberto Francisco21-Jul-10 10:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.