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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:02:31+00:00 2026-05-27T22:02:31+00:00

Can someone share how to practically implement gridview sorting and handle that event if:

  • 0

Can someone share how to practically implement gridview sorting and handle that event if:

  1. The data is binded manually
  2. Gridview is built using template field that is pumped from code behind only (not from the markups)

I build my gridview solely from codebehind therefore I can’t use the default method or solution.

Thank you

  • 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-27T22:02:32+00:00Added an answer on May 27, 2026 at 10:02 pm

    This might be what you are looking for:

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        listBindByName(); //this would be your procedure to look for the data you want
        DataSet dsSortTable = GridView1.DataSource as DataSet;
        DataTable dtSortTable = dsSortTable.Tables[0];
        if (dtSortTable != null)
        {
            DataView dvSortedView = new DataView(dtSortTable);
            dvSortedView.Sort = e.SortExpression + " " + getSortDirectionString();
            ViewState["sortExpression"] = e.SortExpression;
            GridView1.DataSource = dvSortedView;
            GridView1.DataBind();
        }
        UpdatePanel1.Update();
    }
    
    private string getSortDirectionString()
    {
        if (ViewState["sortDirection"] == null)
        {
            ViewState["sortDirection"] = "ASC";
        }
        else
        {
            if (ViewState["sortDirection"].ToString() == "ASC")
            {
                ViewState["sortDirection"] = "DESC";
                return ViewState["sortDirection"].ToString();
            }
            if (ViewState["sortDirection"].ToString() == "DESC")
            {
                ViewState["sortDirection"] = "ASC";
                return ViewState["sortDirection"].ToString();
            }
        }
        return ViewState["sortDirection"].ToString();
    }
    

    This is an example of the TemplateField:

    <asp:TemplateField HeaderText="Description" SortExpression="description">
        <ItemTemplate>
            <asp:Label Visible="true" runat="server" ID="descriptionLabel" Text='<%# bind("description")  %>'></asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox ID="txtEditDescription" Width="100px" runat="server" Text='<%#Bind("description") %>' />
        </EditItemTemplate>
    </asp:TemplateField>
    

    By adding the SortExpression property the GridView header will become clickable. Make sure the sort expression attribute is the name of the field that you are binding through the sql query.

    Hope this helps.

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

Sidebar

Related Questions

Can someone share a simple example of using the foreach keyword with custom objects?
I am trying to implement URL rewriting in my PHP application. Can someone share
Can someone share a regex that find all not double-slashed commented println in java
Can someone share example of using Monkeyrunners broadcastIntent method. To run Browser with certain
I am trying to implement URL rewriting in my PHP application. Can someone share
Can someone share their experiences with s#arp architecture. we have decided to follow mvp
Can someone share a good way to save the program state (UINavigationController stack, etc)
Can someone share how to make items in a ListViewer scrollable? Source code will
Can someone please share a extremely simple version of JQuery Ajax with pagination??
I really appreciate if someone can help me with using how to use shape

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.