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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:11:41+00:00 2026-06-12T15:11:41+00:00

I got the below code from internet. It is working properly. I have added

  • 0

I got the below code from internet. It is working properly. I have added paging also. When I’m just sorting, it is working properly. When I am changing the page index, the sorting is lost.

Here is the client side code that set a gridview with 20 items per page, using the sort linked to the “GridView1_Sorting” method in the server side code.

Client side

<asp:GridView ID="GridView1" runat="server" DataKeyNames="eno" AutoGenerateColumns="False" PageSize="20" AllowPaging="True" AllowSorting="True" OnSorting="GridView1_Sorting" CellPadding="4">
    <Columns>
        <asp:TemplateField HeaderText="Employee no" SortExpression="eno">
            <ItemTemplate>
                <%#Eval("eno")%>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Emp name" SortExpression="empname">
            <ItemTemplate>
                <%#Eval("empname")%>
            </ItemTemplate> 
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Salary" SortExpression="sal">
            <ItemTemplate>
                <%#Eval("sal")%>
            </ItemTemplate> 
        </asp:TemplateField>
    </Columns>
</asp:GridView>

And now the server side code:

Server side

using System.Data.SqlClient;
using System.Configuration;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ConnectionString);
    SqlCommand sqlcmd;
    SqlDataAdapter da;
    DataTable dt = new DataTable();
    DataTable dt1 = new DataTable();

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

    void GridData()
    {
        sqlcmd = new SqlCommand("select * from emp", sqlcon);
        sqlcon.Open();
        da = new SqlDataAdapter(sqlcmd);
        dt.Clear();
        da.Fill(dt);

        if (dt.Rows.Count > 0)
        {
            Session["dt"] = dt;
            GridView1.DataSource = dt;
            GridView1.DataBind();
        } 
    } 

    private string GVSortDirection
    {
        get { return ViewState["SortDirection"] as string ?? "DESC"; }
        set { ViewState["SortDirection"] = value; }
    }

    private string GetSortDirection()
    {
        switch (GVSortDirection)
        { 
            case "ASC":
                GVSortDirection = "DESC";
                break;

            //assign new direction as ascending order
            case "DESC":
                GVSortDirection = "ASC";
                break;
        }

        return GVSortDirection;
    }

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataTable dataTable = (DataTable)Session["dt"];
        if (dataTable != null)
        { 
            DataView dataView = new DataView(dataTable); 
            string sortDirection = GetSortDirection(); 
            dataView.Sort = e.SortExpression + " " + sortDirection; 
            GridView1.DataSource = dataView;
            GridView1.DataBind();
        }
    } 

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridData();
    }
}
  • 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-12T15:11:42+00:00Added an answer on June 12, 2026 at 3:11 pm

    When you page, the PageIndexChanging event is called. This in turn runs the GridData() procedure, which sets the data source for the gridview to be a data table containing records from the emp table, with no particular sort order.

    What you should do is to take the code that you’ve written in your GridView1_Sorting event-handler, and include this within the GridData routine, so that whenever the grid is populated with data – whether when the page first loads, when the page index is changed or when the gridview is sorted – the gridview is based on a sorted dataview, rather than an unsorted data table.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got a code from internet, the code below is picking the contact and
I got the below code from SO expert but it's working for ANSI Strings
I got below code from http://msdn.microsoft.com/en-us/library/dd584174(office.11).aspx for adding custom property in webpart tool pane.
I don't know where I've got a mistake, when I fire below code from
below code is my databasehandler class i got it from a tutorial. Beside that
I've got the code below, and I'm trying to set the from field to
I got the code below from the bitmapmixer sample (DirectShow.NET) and i tried to
i've got the below code, and it operates just fine, only it takes a
I have some code that downloads a file from the internet located here: http://www.amsat.org/amsat/ftp/keps/current/nasa.all
I want to pick photo from contacts . I got below code from stack

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.