ARTICLE

Paging in DataGrid Control using ADO.NET

Posted by Dinesh Beniwal Articles | ADO.NET in VB.NET May 19, 2010
In this article I will explain Paging in DataGrid Control in using ADO.NET.
 
Reader Level:

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.NETWebapplication 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.jpg
 
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.jpg
 
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 Sub DataGrid1_PageIndexChanged(ByVal sourceAs Object,ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs)

        DataGrid1.CurrentPageIndex = e.NewPageIndex

        DataGrid1.DataBind()

    End Sub

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

   Protected Sub Page_Load(ByVal senderAs Object,ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs)

        ' Create a Connection object
       Dim ConnectionStringAs String ="Provider=Microsoft.Jet.OLEDB.4.0;" &"Data Source =c://GuestBook.mdb"
       Dim conn As New OleDbConnection(ConnectionString)

 
       
' open the connection

       If conn.State <> ConnectionState.Open Then
            conn.Open()
       End
If

 
       
' Create a data adapter
       Dim da As New OleDbDataAdapter("SELECT ID,Name,Address,Email FROM Guest", conn)

 
       
' Create and fill a dataset
       Dim ds As New DataSet()
        da.Fill(ds)

 
       
' Bind dataset to the control
        DataGrid1.DataSource = ds
        DataGrid1.DataBind()
       
' Close the connection
       If conn.State = ConnectionState.Open Then
            conn.Close()
       End
If

    End Sub

Now if you run the program, you'll see that the first page looks like figure 7-50.
 
Figure-7.50.jpg
 
Figure 7-50. Paging in the DataGrid
 
As you can see from Figure 7-50, there are five rows displayed in the grid and there are two pages available. If you click on the 2 link, the result looks like figure 7-51.
 
Figure-7.51.jpg
 
Figure 7-51. The second page of DataGrid paging sample
 
Conclusion
 
Hope this article would have helped you in understanding Paging in DataGrid Control using ADO.NET. See other articles on the website also for further reference.

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Become a Sponsor