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

NickMalloySystems LeadAsked:
Who is Participating?
 
BalkisBrCommented:
Oh, sorry, i was mistaken, we'll have to add two properties to control the sorting state.
//Add these properties
public SortDirection StateSorting
        {
            get
            {
                SortDirection? sort = ViewState["StateSorting"] as SortDirection?;
                return sort.HasValue ? sort.Value : SortDirection.Ascending;
            }
            set { ViewState.Add("StateSorting", value); }
        }
 
        public string SelectedSortDirection
        {
            get
            {                                
                    switch (StateSorting)
                    {
                        case SortDirection.Ascending:
                            ViewState.Add("SelectedSortDirection", "DESC");
                            StateSorting = SortDirection.Descending;
                            break;
 
                        case SortDirection.Descending:
                            ViewState.Add("SelectedSortDirection", "ASC");
                            StateSorting = SortDirection.Ascending;
                            break;
                    }
                    return ViewState["SelectedSortDirection"].ToString();
            }            
        }
 
//And replace your method (again)
//...
if (dataTable != null)
            {
                DataView dataView = new DataView(dataTable);
 
                dataView.Sort = e.SortExpression + " " + SelectedSortDirection;
 
                grdOld.DataSource = dataView;
                grdOld.DataBind();
            }
//...

Open in new window

0
 
BalkisBrCommented:
Hi,
You forgot to Set the "e.SortDirection" after change it.
0
 
NickMalloySystems LeadAuthor Commented:
How do you set it?
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
BalkisBrCommented:
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

0
 
NickMalloySystems LeadAuthor Commented:
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();
        }
 

    }
0
 
BalkisBrCommented:
Did you put a break point inside your method?
Your dataTable has value?
0
 
NickMalloySystems LeadAuthor Commented:
yes, everytime it goes through the case SortDirection.Ascending statement.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.