Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6802349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:12:45+00:00 2026-05-26T19:12:45+00:00

//Sort User Table private void SortGridView(string sortExpression, string direction) { DataTable dataTable = BindGridView(Session[useremail].ToString()).Tables[0];

  • 0
//Sort User Table
private void SortGridView(string sortExpression, string direction)
{
    DataTable dataTable = BindGridView(Session["useremail"].ToString()).Tables[0];
    if (dataTable != null)
    {
        DataView dataView = new DataView(dataTable);
        dataView.Sort = sortExpression + direction;

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

protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
{
    string sortExpression = e.SortExpression;
    if (GridViewSortDirection == SortDirection.Ascending)
    {
        GridViewSortDirection = SortDirection.Descending;
        SortGridView(sortExpression, " ASC");
    }

    else
    {
        GridViewSortDirection = SortDirection.Ascending;
        SortGridView(sortExpression, " DESC");
    }
}

public SortDirection GridViewSortDirection
{
    get
    {
        if (ViewState["sortDirection"] == null)
            ViewState["sortDirection"] = SortDirection.Ascending;
        return (SortDirection)ViewState["sortDirection"];
    }
    set { ViewState["sortDirection"] = value; }
}

When I Edit a user and update the edit or do some search, and clear the search the page loads and the sort is lost,

private DataSet BindGridView(string email)
    {
        .......
    }

 protected void btnUpdate_Click(object sender, EventArgs e)
   {
      .....
       BindGridView(Session["useremail"].ToString());
    }

everytime the page loads or some postback is done the sort is lost how to retain the sort.

Page load

 if (PermissionList.Any(item => item.Equals("Edit user")))        
     {            
            if (!IsPostBack)
            {
                BindGridView(Session["useremail"].ToString());
            }
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:12:46+00:00Added an answer on May 26, 2026 at 7:12 pm

    Whenever you perform a new sort on your gridview, store the sort expression in a hidden label, or field, and anytime you re-load/bind your gridview, use your saved sort expression to re-sort the table.

    .aspx

    <asp:Label id="lblHidSortExp" runat="server" visible="false"></asp:Label>
    

    .aspx.cs

    protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sortExpression = lblHidSortExp.Text;
        if(sortExpression == e.SortExpression)
            sortExpression += " DESC";
        else
            sortExpression == e.SortExpression;
    
       //not sure if this is exactly how you get your datatable, but you get the idea
       DataView myView = new DataView(BindGridView(Session["useremail"].ToString()).Tables[0]);
       myView.Sort = sortExpression;
       marksGridView.DataSource = myView;
       marksGridView.DataBind();
    
       //save sort state
       lblHidSortExp.Text = sortExpression;
    }
    

    So say in your update function, use your saved sort exp

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
      .....//do update in db
       //reload your table in dataview
       DataView myView = new DataView(/*load table*/);
       //do sort
       myView.Sort = lblHidSortExp.Text;
       //bind gridview
       marksGridView.DataSource = myView;
       marksGridView.DataBind();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table of the sort: USER | PLAN | START_DATE | END_DATE
I'm trying to sort a user table based on how many comments they have
How can a sort by user popularity when using a hasAndBelongsToMany table relationship? ie:
I know I can use something like User.sort {|a, b| a.attribute <=> b.attribute} or
I am using a dropdown to allow the user to sort search results. These
I can't get my GridView to enable a user to sort a column of
My silverlight app takes the user through a sort of wizard-like process, so for
I have a sort of plug-in model in which various complex user controls are
I am looking at some sort of existing filter which can sanitize the user
I have a gridview and I need to sort its elements when the user

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.