Click here to download the source code for this lesson.
The SqlDataSource server control can make your GridView control easily allow data paging for the end user of your web application without writing any C# code. For example, if you are showing the results of a query on a web page and you want the user to be able to look at one page of data at a time. This of course applies to situations where you have several rows of return data and one screen is not sufficient to display all of the results. You can define how many rows are shown in a page, but by default it is 10. If more rows are in your DataSet than your page size, .NET will show a hyperlink at the bottom of your GridView that allows the user to view page 2, 3, etc.
First, you should have established a binding between the GridView control and an SqlDataSource control on your web form. Refer to the prior lesson Retrieving database data with the ASP.NET SqlDataSource and GridView controls to learn how to do that if you haven’t already.
Next, set the AllowPaging property of your GridView control to True. Then set the PageSize property to how many records you want to show on each page. Again, the default is 10. That is all you have to do. Pretty simple huh?
Click here to watch an example video where I setup paging in my GridView control.
Thanks… Any suggestion or idea how to make a due date reminder?
Comment by Lynn — August 27, 2009 @ 2:12 am
Your example collapses with the following error:
The GridView ‘GridView1′ fired event PageIndexChanging which wasn’t handled
Comment by M — November 19, 2009 @ 9:59 am
Dear M,
You are getting that error “…PageIndexChanging which wasn’t handled” because you need to use the DataSourceID property of the GridView and assign it to a DataSource control like an SqlDataSource or an ObjectDataSource. It’s the DataSource controls that handle the paging automatically. Otherwise you have to implement the event yourself and write code as the error points out. So use an SqlDataSource like I did in the example code.
Comment by ted — November 19, 2009 @ 8:42 pm