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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:01:25+00:00 2026-05-26T07:01:25+00:00

I wanted to sort my asp grid by setting AllowSorting="True". I also added codes

  • 0

I wanted to sort my asp grid by setting AllowSorting="True". I also added codes to the event, however I can’t make it work.

  private void PopulateGridView()
    {
        var a = from c in sample_worker.get()
                select new
                {
                    c.CemID,
                    c.Title,
                    c.Description
                };
        grd_sample.DataSource = a;
        grd_sample.DataBind();

        
    }

this is the code to populate the grid. i added this below the !ispostback..

the code for sorting..

        private string ConvertSortDirectionToSql(SortDirection sortDirection)
    {
        string newSortDirection = String.Empty;

        switch (sortDirection)
        {
            case SortDirection.Ascending:
                newSortDirection = "ASC";
                break;

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

        return newSortDirection;
    }
    protected void grd_sample_Sorting(object sender, GridViewSortEventArgs e)
    {

        DataTable dataTable = grd_sample.DataSource as DataTable;


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

            grd_sample.DataSource = dataView;
            grd_sample.DataBind();
        }

    }

What can I do to fix this? Also I would be able to sort it back and forth? And the if (dataTable != null) is always null.

  • 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-26T07:01:26+00:00Added an answer on May 26, 2026 at 7:01 am

    You have many things wrong:

    1. Your method PopulateGridView binds an IQuerable to your gridview, yet when you handle OnSorting you pretend to be able to get a DataTable as the DataSource of your GridView.

    2. this line: DataTable dataTable = grd_sample.DataSource as DataTable; will always return NULL because your gridview does not keep a reference to the DataSource. You need to refetch the data, sort it and rebind it. In other words, you need to do something like this on grd_sample_Sorting

      protected void grd_sample_Sorting(object sender, GridViewSortEventArgs e)
      {
      var a = from c in sample_worker.get()
                  select new
                  {
                      c.CemID,
                      c.Title,
                      c.Description
                  };
      
          if(e.SortDirection=="ASC")
          {
               if(e.SortExpression=="CemID")
                  a=a.OrderBy(x=>x.CemID);
               else if (e.SortExpression=="Title")
                  a=a.OrderBy(x=>x.Title);
               //And so on...
          }
          else 
          {
               if(e.SortExpression=="CemID")
                  a=a.OrderByDescending(x=>x.CemID);
               else if(e.SortExpression=="Title")
                  a=a.OrderByDescending(x=>x.Title);
               //And so on...
          }
      
          grd_sample.DataSource = a;
          grd_sample.DataBind();
      }
      

    But Frankly, you are probably better off defining a LinqDataSource for your datagridview. LinqDataSource will perform paging, sorting and CRUD operations almost without writing a single line of code. Certainly not for sorting and paging.

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

Sidebar

Related Questions

I wanted to do a sort of animation where a rectangle would move..However, the
I wanted to make Map of Collections in Java, so I can make something
If I wanted to make some sort of api so that ANY mobile game
I wanted to do something like this: <asp:Label ID=lblMyLabel onclick=lblMyLabel_Click runat=server>My Label</asp:Label> I know
I wanted to know if there's a way to add some sort of image
I wanted to let Vim to sort variable declaration like the following: NotifyNotification* notification;
Can anyone tell the function to sort the columns of a gridview in c#
Originally i wanted to ask if it's faster to sort Integers than Strings. But
If I have an array of objects with properties and I wanted to sort
I'm pretty sure what sort of things can be done with NHibernate but I

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.