How to Implement Asp.Net GridView Sorting Example Using C#.Net & VB.Net or how to Use of AllowSorting GridView Property.

In this article i am going to explain about How to Implement Asp.Net GridView Sorting Example Using C#.Net & VB.Net or how to Use of AllowSorting GridView Property. This is achieved by using the AllowSorting and OnSorting properties of the GridView and SortExpression attribute of boundfield if you set AutoGenerateColumns to false.

In my previous article i have explained aboutHow To Implement Asp.Net GridView Pagination Using C#.Net & VB.Net , Convert Dataset Datatable To Json Data Array In Asp.Net C#.Net & VB.Net Or How To Use JavaScriptSerializer To Convert Datatable To Json Data Array ,How To Show Tooltip On Mouse Hover In Gridview In Using JQuery In ASP.Net C#.Net & VB.Net,SQL Script To Drop Multiple Tables,Procedures At Once ,SQL Script To Drop Multiple Tables,Procedures At Once ,Convert Dataset Datatable To Json Data Array,SQL Script To Drop Multiple Tables,Procedures At Once In Sql Server,Highlight,Change Color Of Row On Mouseover In Grid View Using CSS – C#.Net ASP.Net VB.Net and many articles in C#.Net,ASP.Net,VB.Net,Grid View,Javascript,jQuery,SQL Server and many other topics.

I have created a asp.net website and added a web form to the project. I have drag and drop the gridview control from the toolbox and added some styles to gridview to format it. Now i have set AllowSorting Property of C#.Net to true and assigned the OnSorting eventhandler. I set AutoGenerateColumns property to false and defined the columns i want to show along with the sortexpression. The entire html markup is given below.

Html Markup:

Asp.Net GridView Sorting Example Using C#.Net &

VB.Net | Use of AllowSorting GridView Property

Now in the code behind file in the page_load event i have created a datatable and added some records in it. And binded the datatable to gridview. On OnSorting event first i am deciding field field to sort and the direction to sort. Based on it i am creating dataview from the datatable and passing this sorting condition to the dataview and the final output is again binded to the gridview. The entire code is given below.

C#.Net Code:

VB.Net Code:

Now the output will look like below.

Output:


Sourcecode Download:

Sourcecode of How to Implement Asp.Net GridView Sorting Example Using C#.Net & VB.Net or how to Use of AllowSorting GridView Property.

Comments

  1. By Anonymous

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!