ADO.NET Paging DataGrid Control in VB.NET

In this article I will explain Paging in DataGrid Control in using ADO.NET.
  • 4940
  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 AsSystem.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 AsSystem.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.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.