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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:03:17+00:00 2026-05-17T02:03:17+00:00

I have a gridview which is bound to a DataTable. When I try to

  • 0

I have a gridview which is bound to a DataTable. When I try to sort the gridview, it goes blank. How can I enable sorting this gridview?

I know this question has been asked before, but what I’m looking for is an explanation of how to do it. Perhaps with a simple example.

I have read that I need to put some code in the on_sorting and/or on_sorted events, but I don’t understand what needs to go there.

Again, I want to understand the method of accomplishing this, I don’t just want a giant block of code.


Ok, here’s what I have now, but still not working:

            <asp:GridView ID="gridResults" runat="server" CellPadding="4" ForeColor="#333333"
            GridLines="Horizontal" AllowSorting="True" AllowPaging="True" 
            EmptyDataText="No Tracking Information Found for the given criteria." 
            PageSize="15" onsorted="gridResults_Sorted" AutoGenerateColumns="False" 
            EnableSortingAndPagingCallbacks="True">
            <RowStyle BackColor="#E3EAEB" />
            <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#7C6F57" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>

And then in code behind:

  //From search method
gridResults.Columns.Clear();
    foreach (DataColumn col in currentResults.Columns)
    {
        String fieldName = col.ColumnName;
        BoundField field = new BoundField();
        field.DataField = fieldName;
        field.SortExpression = fieldName;
        field.HeaderText = fieldName;

        gridResults.Columns.Add(field);  
    }

    gridResults.DataSource = currentResults;
    gridResults.DataBind();
    gridResults.AllowSorting = true;

Can anyone see what I’m still missing? Results show, but no sorting or paging works.

  • 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-17T02:03:18+00:00Added an answer on May 17, 2026 at 2:03 am

    You are able to make a GridView sortable using a DataTable. All you need to do is make sure that you have Enable Sorting = true and you provide the column name that you wish to be sorted in the SortExpression.

    This is what you would need to do to allow this to be sortable:

    <asp:BoundField HeaderText="Product" 
          DataField="ProductName" SortExpression="ProductName">
    </asp:BoundField>
    

    You always need to set the SortExpression or you are not going to be able to sort your columns

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

Sidebar

Related Questions

I have a GridView , which is bound to a database table. This GridView
I have a DataTable which is bound to a GridView. I also have a
I have a GridView that is bound to a DataTable, which in turn uses
I have a GridView to which I bind a dataTable that I manually created.
I have gridview in my asp.net page which is bound to sqldatasource. when i
I have a bound field in my Gridview which is getting its value from
I have an ItemTemplate Label inside Gridview which I haven't bound with any DataField.
I have a GridView control bound to an ObjectDataSource which returns a list of
I have an ASP.NET GridView that's bound to an ObjectDataSource (which is bound to
In my website, I have a gridview which has datasource dynamically bound from database

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.