This article has been
excerpted from book "A Programmer's Guide to ADO.NET in C#".
The "Paging Property Page" section, the paging page of the Property Builder for
the DataGrid lets you set a DataGrid's paging properties. In this example I'll
show you how you can write applications with paging and navigate though pages in
the DataGrid control. You can set a DataGrid's paging properties in the property
Builder at design-time as well as programmatically. In this section I'll walk
you though both methods.
Note: To test samples, I'll use the Northwind database
To test this application, create an ASP.NET Web application and add a DataGrid
control to the Web form from the toolbox.
Enabling Paging at Design-Time
You can set paging options at design-time by right-clicking on the DataGrid's
properties menu item. The paging page looks like figure 7-48. This page lets you
set the number of rows per page and the type of navigation you want to show. If
you want to provide your own paging click on the Allow Custom Paging check box.

Figure 7-48. DataGrid paging properties
As you can see from figure 7-48, I set the page size to 5 rows and mode to page
Numbers. So, when a browser displays the first page of a table in the DataGrid,
it will show five rows. Additionally, it will display page numbers at the button
of the page so that users can navigate through the pages. The next step for
enabling paging in a data grid is to write a PageIndexChanged event handler. You
can add the PageIndexChanged handler from the data grid's properties windows, as
shown in figure 7-49.

Figure 7-49. Adding the PageIndexChanged event handler
Now write the code shown in listing 7-10. As you can see, I set
DataGrid.CurrentPageIndex as DataGridPageChangedEventArgs's NewPageIndex. VS.NET
takes cure of the rest for you.
Listing 7-10. The PageIndexChanged handler of the data grid control
private void
DataGrid1_PageIndexChanged(object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs
e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.DataBind();
}
The next thing I'm going to do is it fill data from a database. Listing 7-11
shows the code that fills the data from the Northwind database on the form load.
The code should look familiar. I create a connection and data adapter objects
and filled a dataset the Employee table.
Listing 7-11. Source code of DataGrid paging sample




Join the conversation! Your thoughts help the community grow.