GridView Paging Sample in ASP.NET

Here, I will explain how to show a GridView with various types of paging in ASP.NET. We use C# code to bind the SQL data with a GridView control and use the following simple steps to make your ASP.NET GridView control with paging enabled. The GridView control provides you with an easy way to display the number of items on a page without taking much space, with the help of paging. First we create a SQL Server database table.

Creating Table in SQL Server Database

Now create a table named Userinfo with columns UserID, UserName and Country. Set the identity property=true for UserID. The table looks as below.

image1.gif

Now insert data into the table.

image10.gif

First of all drag the GridView control from the Data controls menu. It will add the GridView control's HTML source code as given above.

  1. <asp:GridView ID="GridView2" runat="server">  
  2. </asp:GridView>  

Now add the following namespace.

  1. using System.Data.SqlClient;  
  2. using System.Data;  

Now write the connection string to connect to the database.

  1. string strConnection = "Data Source=.; uid=sa; pwd=wintellect;database=Rohatash;";  

 

Now double-click on the page and write the following code for binding the data with the GridView.

  1. protected void Page_Load(object sender, EventArgs e) {  
  2.     BindData();  
  3. }  
  4. protected void BindData() {  
  5.     string strConnection = "Data Source=.; uid=sa; pwd=wintellect;database=Rohatash;";  
  6.     SqlConnection con = new SqlConnection(strConnection);  
  7.     con.Open();  
  8.     SqlCommand cmd = new SqlCommand("select * from Userinfo", con);  
  9.     DataSet ds = new DataSet();  
  10.     SqlDataAdapter da = new SqlDataAdapter(cmd);  
  11.     da.Fill(ds);  
  12.     GridView1.DataSource = ds;  
  13.     GridView1.DataBind();  
  14.     con.Close();  
  15. }  

Implement PageIndexChanged

Once the event PageIndexChanging is executed, the event will be fired for PageIndexChanged paging. In this event, you can safely bind the grid again to access the second page. It is always preferable to cache the data used to bind the data source.

  1. protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) {  
  2.     GridView1.PageIndex = e.NewPageIndex;  
  3.     BindData();  
  4. }  

There are four types of paging mode styles available in GridView. The following are the names of the types of paging mode styles.

  1. NumericFirstLast
  2. Numeric
  3. NextPrevious
  4. NextPreviousFirstLast

Now click on the GridView control to load the control properties at the right side panel; press F4 and set the following properties of the GridView.

EnablePaging

EnablePaging property to True to get GridView Paging.

PageSize

Number of rows from the data source to display per page.

NumericFirstLast

Now our GridView is ready with data and I will explain how to use each paging option in our GridView. If you observe the code above I have set the PagerSetting property for the GridView. Click on the GridView control and press F4 and set the following properties.

image2.gif

  1. <PagerSettings Mode="NumericFirstLast" PageButtonCount="4" FirstPageText="First" LastPageText="Last"/>  

 

Mode 

This is display type of paging to use.

PageButtonCount 

To display number of pages in the paging.

FirstPageText 

This will display text on the first page Button.

LastPageText 

This will display text on the last page Button.

Now run the application and test it.

image3.gif

Now click on the last Button.

image4.gif

Numeric

By default we will get this numeric paging; once we set AllowPaging and PageSize properties in the GridView to set this property to our GridView write the PagerSetting property like this.

  1. <PagerSettings Mode="Numeric" PageButtonCount="4" />  

 

Now run the application and test it.

image5.gif

NextPrevious

If we want to use this mode for paging in GridView we need to change the PagerSettings mode like this.

NextPageText 

NextPageText property is used for he next page Button.

PreviousPageText 

PreviousPageText property is used for he next page Button.

image6.gif

  1. <PagerSettings Mode="NextPrevious" NextPageText="Next"PreviousPageText="Previous" />  

Now run the application and test it.

image7.gif

NextPreviousFirstLast

If we want to use this mode for paging in GridView we need to change the PagerSettings mode like this.

image8.gif 

  1. <PagerSettings FirstPageText="First" LastPageText="Last"Mode="NextPreviousFirstLast" NextPageText="Next" PreviousPageText="Previous" />  

Now run the application and test it.

image9.gif

Some Helpful Resources

Up Next
    Ebook Download
    View all
    Learn
    View all