How to show 10 next records on DataGrid ?
I let DataGrid show 10 records of my table. when I click the last item in DataGrid , DataGrid will show 10 next records of table. But I don't know how to do this. Please help me ! Thanks a lot !
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Niradhip ChakrabortyPosted Jan 12, 2008, 3:47 PM
I have given the code in asp.net below the same code u can refer for windows form
Bring It On - The Setup
To setup a DataGrid to use the built-in paging function only requires a few property settings. You must set the following:
AllowPaging=True
This tells the DataGrid to use the built-in paging functionality provided by the .NET Framework
PageSize=[number of records per page]
This tells the DataGrid how many records should be displayed on each page
OnPageIndexChanged="[name of page changing event handler]"
This points the DataGrid to the event handler that is called when a page-navigation link is clicked
Listing 1.1 shows the ASP.NET Web Form used to create the page
Listing 1.1 - DataGrid Paging
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
Break It Down
In Listing 1.1 I created a DataGrid and set it to display pages of ten (10) records at a time. It also threw in a few properties to alter the display of the DataGrid . There are two event handlers in this Web Form, the Page_Load() event handler, and the myDataGrid_PageChanger() event handler. It also created another page-level method, the BindData() method. When the page is loaded the first time the BindData() method is called and the DataSet is created and a Customers table is added to it. Then the DataGrid is bound to the Customers table. This will display the first ten (10) records in the Customers table (since I set PageSize=10). If the Page.IsPostBack property is true then the BindData() method is not called from the Page_Load() event handler.
When the page is rendered the first time, ten (10) records are displayed with a series of page-navigation links below them. If one of the page-navigation links is clicked a PostBack is triggered. Since I used an If...Then statement in the Page_Load() event handler, the BindData() method does not get called there. Rather, the BindData() method is called from the myDataGrid_PageChanger() event handler. This is what enables the paging to work properly. Since the myDataGrid_PageChanger() event handler is specified in the OnPageIndexChanged property of the DataGrid, the DataGrid's CurrentPageIndex property is changed by the .NET Framework when a page-navigation link is clicked. The DataSet and DataTable are recreated, and the DataGrid is re-bound to the Customers table, using the CurrentPageIndex value to set the appropriate page to display.
While I used NumericPages as the PagerStyle-Mode property, you could use NextPrev as the property value to display custom text for the next previous buttons. Listing 1.2 shows the same page using the NextPrev value for the PageStyle-Mode property.
Listing 1.2 - Using NextPrev on a DataGrid
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>