1. Back To Blog

ASP.NET interview questions: - Can you explain Method of Sorting GridViewcontrol in ASP.NET?

ASP.NET interview questions: - Can you explain Method of  Sorting  GridViewcontrol in ASP.NET?

Sorting: - Sorting allow you to sort the GridViewcontrol data in Ascending or Descending order.

Let's demonstrate a simple example to see how exactly we can sort the GridView Control.

In order to sort the GridView control you need to follow the following steps.

Step1: - Create a new Project > Go to file > New > Project > ASP.NET Empty Web Application.

Now, Add a Web Form in to your application.

Go to Solution explorer > Right click on the project name> Select Add > Add New Item > Select Web Form and click Add.

Step2: -Now on the WebForm1 design modes just drag and drop GridView control from the ToolBox.

In the above diagram of GridView Control you see that the circled column is inblack color, now just go to properties of GridView control set AllowSorting to true and also set AutoGenerateColumns to false.

As soon as you set Allowsorting to true will find the GridView control like below diagram.

In the above diagram you see that now the color of the circle columns is in blue color with underline on it this indicates that you can now sort the GridView control.

Step3: -Go to source of the page and add the following in to it.

In the above code snippet you can see that I have called GridView1_Sorting event on OnSorting property and in the column BoundField just have allow SortExpression on StudentName column.

Step4: -Now go to Webform1.aspx.cs and add the below code snippet.

Once you have done with the above steps now run your application and will see output like below diagram.

The result above is displayed according to the data in theSQLServer table, now just click on StudentName and will find result like below diagram.

Now, you can see that GridViewdata has been sorted in Descending order and if you again click on StudentName now the data will be sorted in Ascending order.

Following is the video on ASP.NET Authentication,Authorization,Principal and Identity objects: -


Click and get more stuffs on ASP.NET interview questions


Visit for more author's article onASP.NET interview questions

Shiv Prasad Koirala

Visit us @ www.questpond.com or call us at 022-66752917... read more

We are on Social