?
Solved

ASP.NET(C#) sorting and paging GridView problem

Posted on 2009-12-21
3
Medium Priority
?
680 Views
Last Modified: 2012-05-08
Hello,

I have the following code which enables paging and sorting in an asp.net framework 3.5 gridview control.

It works fine, I can page through multiple pages select the item I need and load it into a detailsView control.

However, the sort seems to only re-arrange the order of the items in a GridView, which becomes problematic when I select an item from my sorted GridView. The selected item becomes the item that is in that slot in the original, unsorted data set. I need to select the new, sorted row.

To clarify, an example:
Unsorted:
Row 1
Row 2
Row 3
Row 4

a sort will yield:

Row 4
Row 3
Row 2
Row 1
With the sorted GridView I now select Row 4, but the selected KeyField value of the row I am clicking on is actually still pointing at Row 1.

What should I do to my code to achieve a true sort?

Thanks!
protected void LocGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        //all the code that is needed for paging
        LocGrid.PageIndex = e.NewPageIndex;
        LocGrid.DataBind();

        //clear selected index
        LocGrid.SelectedIndex = -1;
      

    }


   private string getSortDirectionString(SortDirection sortDirection)
    {
        
        //this will save the most recently used sort direction in a viewstate variable and then toggle it 
        string newSortDirection = String.Empty;
        SortDirection newSortOrder = SortDirection.Ascending;

        if (ViewState["PreviousSortDirection"] == null)
        {
            newSortDirection = "ASC";
        }
        else
        {
            SortDirection sortDir = (SortDirection)ViewState["PreviousSortDirection"];
            if (sortDir == SortDirection.Ascending)
            {
                newSortDirection = "DESC";
                newSortOrder = SortDirection.Descending;
            }
            else
            {
                newSortDirection = "ASC";
                newSortOrder = SortDirection.Ascending;
            }
        }

        ViewState["PreviousSortDirection"] = newSortOrder;
        return newSortDirection;
    }

    protected void LocGrid_Sorting(object sender, GridViewSortEventArgs e)
    {
        //user clicks on column header to initiate sorting, which leads to this code with enables sorting
        DataTable dt = LocGrid.DataSource as DataTable;

        if (dt != null)
        {
            DataView dv = new DataView(dt);
            //pass previously used sort direction and toggle each time a user clicks
            dv.Sort = e.SortExpression + " " + getSortDirectionString(e.SortDirection);

            LocGrid.DataSource = dv;
            LocGrid.DataBind();

            
        }
    }
    protected void LocGrid_Sorted(object sender, EventArgs e)
    {
               
        //clear selected index
        LocGrid.SelectedIndex = -1;

        
    }

Open in new window

0
Comment
Question by:dantcho
  • 2
3 Comments
 
LVL 7

Accepted Solution

by:
urir10 earned 1500 total points
ID: 26098377
i think whats happening here is that once you set a new page index it does reapply the sorting properly.
The way i do it is i reapply the sorting together with the new page index. So something like this
        DataView dataView = new DataView();
        string sortDirection = "ASC";
        if (Session["SortDir"] != null)
            sortDirection = Session["SortDir"].ToString();
        string sortExpression = "Status";
        if (Session["SortExpr"] != null)
            sortExpression = Session["SortExpr"].ToString();


        if (Session["results"] != null)
        {
           
            grv_Tickets.PageIndex = e.NewPageIndex;
            dataView.Sort = sortExpression + " " + sortDirection;

            grv_Tickets.DataSource = dataView;

            grv_Tickets.DataBind();

        }

Open in new window

0
 

Author Comment

by:dantcho
ID: 26098432
//DataTable dt = LocGrid.DataSource as DataTable;
 DataTable dt = dbLoc.Fetch();

Fixed my problem just needed to make the above change in the snipped I posted. Enjoy the free points :)
0
 

Author Closing Comment

by:dantcho
ID: 31668630
fixed my own problem, never got to test poster's solution/suggestion
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

862 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