Sorting , Paging and AutoGenerateColumns

The GridView control offers a comprehensive array of built-in functionalities, empowering users with the ability to efficiently perform sorting, updating, deleting, selecting, and paging operations on the items contained within the control. These capabilities provide a seamless and intuitive user experience, allowing for easy manipulation and management of data displayed within the GridView. Whether it's organizing data based on specific criteria, modifying existing records, removing unwanted entries, selecting particular items of interest, or navigating through multiple pages of content, the GridView control streamlines these tasks, enhancing user productivity and interaction.

Gridview paging sorting

Download Database

In this article I have used Microsoft's Pubs database for sample data. You can download it free from the following link.


Before you start to generate GridView in your asp file, you should create a ConnectionString in your web.Config File. Double click the web.config file on the right hand side of the Visual Studio and add the following connectionstring code in that file.

web.config file

Web.Config File

<?xml version="1.0"?> <configuration> <connectionStrings> <add name="SQLDbConnection" connectionString="Server=Your-Server-Name; Database=pubs; User Id=sa; password= your-passoword" providerName="System.Data.SqlClient" /> </connectionStrings> </configuration>

Within the GridView control, each column is represented by a DataControlField object. By default, the AutoGenerateColumns property is set to true, which allows the GridView control to automatically generate columns based on the data source. This automatic generation is convenient when you want to quickly display all available fields.

However, if you prefer more control over the appearance and arrangement of columns, you can manually specify the column fields by setting the AutoGenerateColumns property to false. This gives you the flexibility to define the columns explicitly and customize their properties according to your requirements. By disabling the automatic generation of columns, you can tailor the GridView layout to match your desired presentation style and achieve a more personalized and precise representation of your data.


Sorting allows the user to sort the items in the GridView control with respect to a specific column by clicking on the column's header. To enable sorting, set the AllowSorting property to true.


To enhance the usability and manageability of large datasets, the GridView control offers a convenient paging feature. Instead of displaying all records from the data source simultaneously, the GridView control can automatically divide them into separate pages.

To enable paging functionality, you can set the AllowPaging property of the GridView control to true. By doing so, the GridView control will automatically generate pagination controls, such as next and previous buttons or page numbers, allowing users to navigate through different pages of data.

This feature is particularly useful when dealing with a significant amount of information, as it improves performance by reducing the load on the page and enhances user experience by presenting data in a more organized and digestible manner. With paging enabled, users can easily browse through different pages of records within the GridView control, simplifying navigation and ensuring efficient data retrieval.


Also we can set how many rows we want to see in a page.


The following ASP.NET program shows how to enable sorting and paging in a GridView with a custom defined column fields.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""> <html xmlns=""> <head id="Head1" runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowSorting="True" DataSourceID="SqlDataSource1" AllowPaging="True" PageSize="4"> <Columns> <asp:BoundField ReadOnly="True" HeaderText="stor_id" DataField="stor_id" SortExpression="stor_id"></asp:BoundField> <asp:BoundField HeaderText="stor_name" DataField="stor_name" SortExpression="stor_name"></asp:BoundField> <asp:BoundField HeaderText="stor_address" DataField="stor_address" SortExpression="stor_address"></asp:BoundField> <asp:BoundField HeaderText="city" DataField="city" SortExpression="city"></asp:BoundField> <asp:BoundField HeaderText="state" DataField="state" SortExpression="state"></asp:BoundField> <asp:BoundField HeaderText="zip" DataField="zip" SortExpression="zip"></asp:BoundField> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SQLDbConnection %>" SelectCommand="select * from stores" /> </div> </form> </body> </html>