Thursday, December 05, 2013

Simple paging for gridview asp.net C#

How to create a simple paging for your grid-view. 

Assuming you already have everything in place, without the paging.

Firstly, insert this in your page that requires paging. In this case, it will be manageBook.aspx as it is a book management system.

 <asp:GridView ID="grdData" runat="server" DataKeyNames="BookRecordId" AutoGenerateColumns="false"   
   OnRowCommand="BookManage" AllowPaging="True" PageSize="4"   
   onpageindexchanging="grdData_PageIndexChanging" PagerSettings-Mode="NumericFirstLast"  
   PagerSettings-Visible="True">  

Next, in your code behind file, manageBook.aspx.cs, insert this method.

 protected void grdData_PageIndexChanging(object sender, GridViewPageEventArgs e)  
     {  
       DataTable dtBook = new DataTable();  
       CBookManager BooksManager = new CBookManager();  
       dtBook = BooksManager.getAllBookData();  
       grdData.DataSource = dtBook;  
       grdData.PageIndex = e.NewPageIndex;  
       grdData.DataBind();  
     }  

That's All :D Any mistakes in the tutorial, do comment below so that I can change it :) Thanks

No comments: