In this article I will explain a tutorial with example, how to implement Paging and Sorting using SqlDataSource in ASP.Net GridView.
ASP.Net GridView has inbuilt Paging and Sorting capabilities which can be easily enabled when SqlDataSource control is used.
Here I am making use of Microsoft’s Northwind Database. The download and install instructions are provided in the following article.
Implement Paging and Sorting in ASP.Net GridView using SqlDataSource
1. Drag a GridView control to the page.
2. Now in the Design View of the page, click on the small Arrow on the right side of GridView to open the GridView Tasks popup.
Click on Choose Data Source DropDown and select <New data source…> option.
3. In the Data Source Configuration Wizard, select SQL Database, specify the ID of the SqlDataSource control and click the OK button.
4. Now click on the New Connection button to set the Connection String to the Northwind Database.
The New Connection button opens the Add Connection dialog where you will need to select the SQL Server instance and the database. Once the appropriate selections are done, click on the Test Connection button to test the connection.
Finally click on the OK button.
5. Now the Wizard will ask you to set the name of the Connection String setting in the Web.Config file. After setting the desired name, click the Next button.
6. Then you will need to choose whether you will use a Stored Procedure or SQL Statement. In case an SQL Statement is used, you need to choose and select the columns that must be displayed in the GridView.
There are also other options such as adding WHERE condition or ORDER BY clause to the SQL Statement.
7. In the last step of the Wizard, the records fetched from the SELECT statement will be previewed and finally you will need to click the Finish button to complete the configuration of the SqlDataSource.
8. Now your SqlDataSource is properly configured and now the last step is to add Paging and Sorting capabilities to the GridView control. In order to add Paging and Sorting to GridView, again you will need to click on the arrow to the right of the GridView in Design view of the page and check the Paging and Sorting checkboxes.
That’s all you need to do in order to enable Paging and Sorting to your GridView which is populated using SqlDataSource control in ASP.Net.