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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:52:35+00:00 2026-05-17T19:52:35+00:00

I have a gridview where I bind a datasource, and I had to add

  • 0

I have a gridview where I bind a datasource, and I had to add sorting for this gridview; I added the code below to that, but it didn’t work well.

private string ConvertSortDirectionToSql(SortDirection sortDireciton)
{
    string m_SortDirection = String.Empty;

    switch (sortDireciton)
    {
        case SortDirection.Ascending:
            m_SortDirection = "ASC";
            break;

        case SortDirection.Descending:
            m_SortDirection = "DESC";
            break;
    }

    return m_SortDirection;
}

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
    DataTable m_DataTable = GridView1.DataSource as DataTable;

    if (m_DataTable != null)
    {
        DataView m_DataView = new DataView(m_DataTable);
        m_DataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

        GridView1.DataSource = m_DataView;
        GridView1.DataBind();
    }
}
  • 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-17T19:52:36+00:00Added an answer on May 17, 2026 at 7:52 pm

    You can use this, as I had the same problem, and I solved it like this.

    public string SortingExpression
    {
        get
        {
            if (this.ViewState["SortExpression"] == null)
                return "";
            else
                return (string)this.ViewState["SortExpression"];
        }
    
        set
        {
            this.ViewState["SortExpression"] = value;
        }
    }
    
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataTable m_DataTable = GridView1.DataSource as DataTable;
    
        if (m_DataTable != null)
        {
            DataView m_DataView = new DataView(m_DataTable);
            SortingExpression = e.SortExpression + " " + (SortingExpression.Contains("ASC") ? "DESC" : "ASC");
            m_DataView.Sort =SortingExpression;
    
            GridView1.DataSource = m_DataView;
            GridView1.DataBind();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a gridview that is using a LinqDataSource for it's datasource. I've added
I have a ASP.net gridview that I am trying bind to. My DataSource has
I have a GridView that I would like to bind to an ObjectDataSource. I
I have a GridView to which I bind a dataTable that I manually created.
I have this code which export data from GridView to csv. It works with
I have this code: Dim Result As New DataTable DataAdapter.Fill(Result) 'bind data to visible
I have a gridview that I bind with the result of a linq query
I have a GridView on an ASP.NET page that I'm trying to bind to
I have a gridview and a datsource. There is one column in datasource that
I have a VS2010 ASP.Net webform that contains a GridView. This GridView has ShowHeaderWhenEmpty=True

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.