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 8561733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:34:07+00:00 2026-06-11T16:34:07+00:00

I have a gridview that is in an updatepanel. The sort works fine when

  • 0

I have a gridview that is in an updatepanel. The sort works fine when you click the column headers but if I go to the next page, the sort is lost. I will provide the code below and if ANYONE out there has an idea on how to get out of this mess, please help!

The ShowGrid() function is called in the Page_Load. I am guessing the problem lies with recalling ShowGrid() in the paging function.

 //Show Grid based on argument type
protected void ShowGrid(string arg)
{
    //dataset to hold email addresses
    DataSet ds = new DataSet();
    try
    {
        //open connection with new connection string            
        conn.Open();

        String selectString = String.Format("SELECT * FROM hr_OnlineJobApp"
                     + " WHERE adminCategory='" + adminCategory + "'"
                     + "AND departmentApplyingFor LIKE '" + dept + "' ORDER BY {0} {1}", ViewState["sortExp"], ViewState["sortOrder"]);
        SqlCommand cmd = new SqlCommand(selectString, conn);


        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
        adapter.Fill(ds);

        //bind to gridview
        GridView1.DataSource = ds;
        GridView1.DataBind();

        GridView1.PageIndex = Convert.ToInt32(ViewState["pageIndex"]);
    }
    finally
    {
        if (conn != null)
            conn.Close();
    }
}
 /**********************************************
 * Paging functionality 
 * *******************************************/
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    //reshow Grid with current type
    adminCategory = GridView1.DataKeys[0]["adminCategory"].ToString();
    ViewState["pageIndex"] = e.NewPageIndex.ToString();
    ShowGrid(adminCategory);
}

/**********************************************
 * Sorting functionality 
 * *******************************************/
protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
{
    string sortExp = e.SortExpression;
    ViewState["sortExp"] = sortExp;
    string sortDir = (string)ViewState["sortOrder"];

    //Changes the sortDir
    if (ViewState["sortOrder"].ToString() == "desc")
    {
        ViewState["sortOrder"] = "asc";
    }
    else
    {
        ViewState["sortOrder"] = "desc";
    }

    try
    {
        conn.Open();
        string sql = "SELECT * FROM hr_OnlineJobApp"
                     + " WHERE adminCategory='" + adminCategory + "'"
                     + "AND departmentApplyingFor LIKE '" + dept + "' ORDER BY " + sortExp + " " + sortDir;


        SqlDataAdapter mySqlAdapter = new SqlDataAdapter(sql, conn);
        DataSet myDataSet = new DataSet();
        mySqlAdapter.Fill(myDataSet);


        GridView1.DataSource = myDataSet;
        GridView1.DataBind();
    }
    finally
    {
        conn.Close();
    }
}
  • 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-06-11T16:34:09+00:00Added an answer on June 11, 2026 at 4:34 pm

    You are setting the PageIndex to 0 in Showgrid:

    GridView1.PageIndex = 0;
    

    Therefore you’re overwriting the new value from PageIndexChanging

    GridView1.PageIndex = e.NewPageIndex;
    

    So you just don’t need to set the PageIndex in ShowGrid to fix the issue.

    Another possible reason for the issue:

    Maybe you’re databinding the GridView on every postback from Page_Load. You should do that only at the first time. Use the Page.IsPostBack property:

    protected void Page_Load(object sender, System.EventArgs e)
    {
       if(!Page.IsPostBack)
           ShowGrid();
    }
    

    Last but not least: you are sorting only if the user clicks on the header in gridView_Sorting. You should apply the ORDER BY also in ShowGrid or use only one method to databind your GridView. But you’re always ordering by date desc there.

    You should not use a DataView to apply the sort, instead you should use the ORDER BY in the sql at the first place.

    String sql = String.Format("SELECT * FROM hr_OnlineJobApp"
                             + " WHERE adminCategory='"+adminCategory+"'"
                             + " ORDER BY {0} {1}", sortExp, sortDir); 
    SqlCommand mySqlCommand = new SqlCommand(sql, conn);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a asp gridview with button field column on that button click I
My Gridview won't page or sort. By this I mean that the data in
Basically I have a gridview that I page through the server instead of in
I have a simple situation that does not work. Gridview is nested inside updatepanel
I have a gridview inside an updatepanel and I have a javascript that calls
i have a gridview in updatePanel that i need to update every interval of
I have a gridview that is within an updatepanel for a modal popup I
I have a GridView that is inside of an UpdatePanel. I am using the
I have an gridview that I am adding onclick events to a checkbox column
I have a Gridview that displays a huge list of products. On Client click

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.