Link to home
Start Free TrialLog in
Avatar of NickMalloy
NickMalloyFlag for United States of America

asked on

GridView Sorting not working properly

My gridview is not sorting Desc. It always goes to Asc. I fill the Grid using a DataTable. here is my sort function. Am I missing something?
private string grdSortDirection(SortDirection sortDirection)
    {
        string newSortDirection = String.Empty;
 
        switch (sortDirection)
        {
            case SortDirection.Ascending:
                newSortDirection = "DESC";
                break;
 
            case SortDirection.Descending:
                newSortDirection = "ASC";
                break;
        }
 
        return newSortDirection;
    }
 
    
    protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataTable dataTable = grdOld.DataSource as DataTable;
 
        if (dataTable != null)
        {
            DataView dataView = new DataView(dataTable);
            dataView.Sort = e.SortExpression + " " + grdSortDirection(e.SortDirection);
 
            grdOld.DataSource = dataView;
            grdOld.DataBind();
        }
    }

Open in new window

Avatar of BalkisBr
BalkisBr

Hi,
You forgot to Set the "e.SortDirection" after change it.
Avatar of NickMalloy

ASKER

How do you set it?
replace the code, and remove your function.
if (dataTable != null)
            {
                DataView dataView = new DataView(dataTable);
 
                string newSortDirection;
                switch (e.SortDirection)
                {
                    case SortDirection.Ascending:
                        newSortDirection = "DESC"; e.SortDirection = SortDirection.Descending;
                        break;
 
                    case SortDirection.Descending:
                        newSortDirection = "ASC"; e.SortDirection = SortDirection.Ascending;
                        break;
                }
 
                dataView.Sort = e.SortExpression + " " + newSortDirection;
 
                grdOld.DataSource = dataView;
                grdOld.DataBind();
            }

Open in new window

I changed my sort function to what you had, and now It sorts DESC and that is it

protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataTable dataTable = grdOld.DataSource as DataTable;

        if (dataTable != null)
        {
            DataView dataView = new DataView(dataTable);

            string newSortDirection = "";
            switch (e.SortDirection)
            {
                case SortDirection.Ascending:
                    newSortDirection = "DESC"; e.SortDirection = SortDirection.Descending;
                    break;

                case SortDirection.Descending:
                    newSortDirection = "ASC"; e.SortDirection = SortDirection.Ascending;
                    break;
            }

            dataView.Sort = e.SortExpression + " " + newSortDirection;

            grdOld.DataSource = dataView;
            grdOld.DataBind();
        }
 

    }
Did you put a break point inside your method?
Your dataTable has value?
yes, everytime it goes through the case SortDirection.Ascending statement.
ASKER CERTIFIED SOLUTION
Avatar of BalkisBr
BalkisBr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial