Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

GridView Sorting not working properly

Posted on 2009-04-16
7
Medium Priority
?
389 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:NickMalloy
  • 4
  • 3
7 Comments
 
LVL 3

Expert Comment

by:BalkisBr
ID: 24160973
Hi,
You forgot to Set the "e.SortDirection" after change it.
0
 

Author Comment

by:NickMalloy
ID: 24161033
How do you set it?
0
 
LVL 3

Expert Comment

by:BalkisBr
ID: 24161262
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:NickMalloy
ID: 24161338
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
 
LVL 3

Expert Comment

by:BalkisBr
ID: 24161540
Did you put a break point inside your method?
Your dataTable has value?
0
 

Author Comment

by:NickMalloy
ID: 24161766
yes, everytime it goes through the case SortDirection.Ascending statement.
0
 
LVL 3

Accepted Solution

by:
BalkisBr earned 500 total points
ID: 24162210
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses

564 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