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

  • Home
  • SEARCH
  • 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 6115603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:06:38+00:00 2026-05-23T15:06:38+00:00

I have an exisiting gridview that I need to fix/improve here at work. Basically

  • 0

I have an exisiting gridview that I need to fix/improve here at work. Basically the GridView has headings and they are being binded by a data set in the code behind, it uses BoundFields and TemplateFields. The problem is that I am needed to make each column sortible. What would be the best approach to do this since it is not the standard gridview?, I need to make the header links and when click to sort in DESC or ASC order. Here is an example of the gridview I need to work on.

<asp:GridView ID="theGrid" runat="server" CssClass="Grid" AllowPaging="True" AutoGenerateColumns="False" CellPadding="4" ForeColor="#FFFFFF" GridLines="None" OnPageIndexChanging="theGrid_PageIndexChanging" CaptionAlign="Left" OnRowCommand="theGrid_RowCommand" Width="100%" PageSize="25" AllowSorting="True" EmptyDataText="It's Empty.">
    <Columns>
        <asp:BoundField DataField="TestID" HeaderText="ID"/>
        <asp:TemplateField HeaderText="Name">
            <ItemTemplate>
                <a href='sometest.aspx?ID=<%# DataBinder.Eval(Container.DataItem, "TestID").ToString()%>'><%# DataBinder.Eval(Container.DataItem, "Type").ToString()%></a>
            </ItemTemplate> 
        </asp:TemplateField>    
          <asp:TemplateField HeaderText="Address">
            <ItemTemplate>
                <div align="right"><%# (DataBinder.Eval(Container.DataItem, "HouseNumber"))%></div>
            </ItemTemplate> 
        </asp:TemplateField>  

…etc, What would be the best way to sort BoundFields and TemplateFields?

  • 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-23T15:06:38+00:00Added an answer on May 23, 2026 at 3:06 pm

    You need to set the SortExpression.

    <asp:TemplateField HeaderText="Name" SortExpression="Name">
        <ItemTemplate>
            <a href='sometest.aspx?ID=<%# DataBinder.Eval(Container.DataItem, "TestID").ToString()%>'><%# DataBinder.Eval(Container.DataItem, "Type").ToString()%></a>
        </ItemTemplate> 
    </asp:TemplateField>    
    <asp:TemplateField HeaderText="Address" SortExpression="Address">
        <ItemTemplate>
                    <div align="right"><%# (DataBinder.Eval(Container.DataItem, "HouseNumber"))%></div>
        </ItemTemplate> 
    </asp:TemplateField>            
    

    in the codebehind you can store the current SortExpression and the SortDirection in ViewState:

    Private Property SortExpression() As String
        Get
            If ViewState("SortExpression") Is Nothing OrElse ViewState("SortExpression").ToString().Length = 0  Then
                ViewState("SortExpression") = "Name ASC"
            End If
            Return ViewState("SortExpression").ToString
        End Get
        Set(ByVal value As String)
            ViewState("SortExpression") = value
        End Set
    End Property
    

    and in the Sorting-Handler of the GridView:

    Protected Sub theGrid_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles theGrid.Sorting
        Dim currentSortColumn, currentSortDirection As String
        currentSortColumn = Me.SortExpression.Split(" "c)(0)
        currentSortDirection = Me.SortExpression.Split(" "c)(1)
    
        If e.SortExpression.Equals(currentSortColumn) Then
            'switch sort direction
            Select Case currentSortDirection.ToUpper
                Case "ASC"
                    Me.SortExpression = currentSortColumn & " DESC"
                Case "DESC"
                    Me.SortExpression = currentSortColumn & " ASC"
            End Select
        Else
            Me.SortExpression = e.SortExpression & " ASC"
        End If
    
        BindGrid() 'load the data with this SortExpression and DataBind the Grid'
    End Sub
    

    Sorry for VB.NET, i’ve noticed too late. You could translate it here.

    Edit: C#

    private string SortExpression {
        get {
            if (ViewState("SortExpression") == null || ViewState("SortExpression").ToString().Length == 0) {
                ViewState("SortExpression") = "Name ASC";
            }
            return ViewState("SortExpression").ToString;
        }
        set { ViewState("SortExpression") = value; }
    }
    
    
    
    protected void theGrid_Sorting(object sender, System.Web.UI.WebControls.GridViewSortEventArgs e)
    {
        string currentSortColumn = null;
        string currentSortDirection = null;
        currentSortColumn = this.SortExpression.Split(' ')[0];
        currentSortDirection = this.SortExpression.Split(' ')[1];
    
        if (e.SortExpression.Equals(currentSortColumn)) {
            //switch sort direction
            switch (currentSortDirection.ToUpper()) {
                case "ASC":
                    this.SortExpression = currentSortColumn + " DESC";
                    break;
                case "DESC":
                    this.SortExpression = currentSortColumn + " ASC";
                    break;
            }
        } else {
            this.SortExpression = e.SortExpression + " ASC";
        }
    
        //load the data with this SortExpression and DataBind the Grid
        BindGrid();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have existing java code and need to create Design Document based on that.
I have a fairly large work project that uses pygtk for the GUI and
I have a problem with Gridview sorting that is similar to others but I'm
I have a GridView that allows editing the values in every column, in every
Need help pretty soon on this..... I have a Gridview in which the columns
I have existing code that uses CMNewProfileSearch to find then iterate over the color
I have existing Linux shared object file (shared library) which has been stripped. I
For whatever reason, I have a lot of clients that have existing data that's
We have an existing WCF service that makes use of wsDualHttpBinding to enable callbacks
I need to add a Company Name field that is associated with the logins.

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.