Web Studyaspnet.blogspot.com Babyneed.blogspot.com

ASP.Net Web Developer Guide

Power By: eXTReMe Tracker Powered by Blogger  
 
 
 
 

 

 
Important Links
 
   
     
 
Sitemap
 
   
   
 
Reference
 
   

Wednesday, March 29, 2006

Providing Paging in DataGrid

In case of a large data table, we may want to provide paging capability to the user.We implement the paging functionality in many different ways. In this context, we will present two examples. First, we will illustrate how to provide a pair of VCR style icons to enable the user to navigate to the previous or the next page of the data displayed in a data grid. Later, we will present an example that will show how to enable the user to navigate to a particular desired page.
Using Previous Page and Next Page Icons
The run-time view of this application is shown Figure 3.82.To accomplish the paging, we have set the following properties of the data grid:
  • AllowPaging=“true”

  • PageSize=“5”

  • PagerStyle-HorizontalAlign=“Center”

  • OnPageIndexChanged=“doPaging
Figure 3.82 Using VCR Style Icons for Page Navigation


The data grid automatically generates the previous page and next page icons. When any one of these icons is clicked, the doPaging subprocedure is triggered. The click event passes a DataGridPageChangedEventArgs parameter to the subprocedure. In the doPaging procedure we have set the currentPageIndex property of the data grid to the newPageIndex property of this parameter.Then we issued a call to the bindDataGrid procedure as shown in the following code excerpt.The complete code for this application is shown in Figure 3.83 and can be found on the CD that accompanies this book in the file named DataGrid4.aspx.
Sub doPaging(s As Object, e As DataGridPageChangedEventArgs)
dataGrid1.CurrentPageIndex=e.NewPageIndex
bindDataGrid
End Sub
Figure 3.83 DataGrid4.aspx
<!— Chapter3/DataGrid4.aspx —>
<%@ Page Language="VB" Debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script language="VB" Debug="true" runat="server">
Sub Page_Load(Source As Object, E As EventArgs)
If Not IsPostBack Then
bindDataGrid
End If
End Sub
Sub bindDataGrid
Dim myConn As OleDbConnection
Dim myOleDbAdapter As OleDbDataAdapter
Dim connStr, sqlStr As String
Dim myDataSet As New Dataset
connStr="Provider=Microsoft.Jet.OLEDB.4.0; " _
+ "Data Source=D:\Products.mdb"
sqlStr="SELECT ProductId, ProductName, Price " _
+ "FROM Products ORDER BY ProductId"
myConn= New OleDbConnection(connStr)
myConn.Open()
myOleDbAdapter=New OleDbDataAdapter(sqlStr,myConn)
myOleDbAdapter.Fill(myDataSet,"dtProducts")
dataGrid1.DataSource=myDataSet.Tables("dtProducts")
dataGrid1.DataBind()
myConn.Close()
End Sub
Sub doPaging(s As Object, e As DataGridPageChangedEventArgs)
dataGrid1.CurrentPageIndex=e.NewPageIndex
bindDataGrid
End Sub
</script>
<html><head></head><form runat="server">
<asp:DataGrid runat="server" id="dataGrid1" AutoGenerateColumns="true"
AllowPaging="true" PageSize="5" PagerStyle-HorizontalAlign="Center"
OnPageIndexChanged="doPaging" BackColor="White" BorderWidth="1px"
BorderStyle="Solid" Width="100%" BorderColor="Salmon"
CellPadding="2" CellSpacing="0" Font-Name="Verdana" Font-Size="8pt">
<HeaderStyle Font-Size="8" Font-Names="Arial" Font-Bold="True"
BackColor="Yellow" HorizontalAlign="center">
</HeaderStyle>
<AlternatingItemStyle BackColor="Beige"/>
</asp:DataGrid>
</center></form></html>

0 Comments:

Post a Comment

<< Home