In this article we
will learn to do Paging in a DataGrid in two different modes.
- NextPrevious Mode
- NumericPages Mode
Program
Default.aspx code
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Paging
in a Datagrid</title>
<style type="text/css">
.hr1
{
color: #f00;
background-color: #f00;
height: 5px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<hr class="hr1" />
<asp:Label ID="lb1" Text="NextPrevious
Mode" BackColor="Yellow" ForeColor="Red"runat="server"></asp:Label>
<asp:DataGrid id="DataGrid1" BackColor="#FF9999" AllowPaging="True" PageSize="3"OnPageIndexChanged="datagrid1_pageindexchanged" PagerStyle-Mode="NextPrev" runat="server"Width="328px">
<HeaderStyle BackColor="#FFCC99" />
</asp:DataGrid>
<hr class="hr1" />
<asp:Label ID="Label1" Text="NumericPages
Mode" BackColor="Yellow" ForeColor="Red"runat="server"></asp:Label>
<asp:DataGrid id="DataGrid2" BackColor="#FF9999" AllowPaging="True" PageSize="3" OnPageIndexChanged="datagrid2_pageindexchanged" PagerStyle-Mode="NumericPages"runat="server" Width="328px">
<HeaderStyle BackColor="#FFCC99" />
</asp:DataGrid>
<hr class="hr1" />
</div>
</form>
</body>
</html>
Default.aspx.vb code
Imports System.Data.SqlClient
Imports System.Data
Partial Class _Default
Inherits System.Web.UI.Page
Dim strConnString As String =
System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ToString()
Dim con As New SqlConnection(strConnString)
Dim sqlda As SqlDataAdapter
Dim com As SqlCommand
Dim ds As DataSet
Dim str As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) HandlesMe.Load
If Not IsPostBack Then
bindgrid1()
bindgrid2()
End If
End Sub
Sub bindgrid1()
Try
con.Open()
str = "select
* from student"
com = New SqlCommand(str,
con)
sqlda = New SqlDataAdapter(com)
ds = New DataSet
sqlda.Fill(ds, "student")
DataGrid1.DataSource = ds
DataGrid1.DataMember = "student"
DataGrid1.DataBind()
con.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Sub bindgrid2()
Try
con.Open()
str = "select
* from student"
com = New SqlCommand(str,
con)
sqlda = New SqlDataAdapter(com)
ds = New DataSet
sqlda.Fill(ds, "student")
DataGrid2.DataSource = ds
DataGrid2.DataMember = "student"
DataGrid2.DataBind()
con.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Protected Sub datagrid1_pageindexchanged(ByVal source As Object, ByVal e AsSystem.Web.UI.WebControls.DataGridPageChangedEventArgs) HandlesDataGrid1.PageIndexChanged
DataGrid1.CurrentPageIndex = e.NewPageIndex
bindgrid1()
End Sub
Protected Sub datagrid2_pageindexchanged(ByVal source As Object, ByVal e AsSystem.Web.UI.WebControls.DataGridPageChangedEventArgs) HandlesDataGrid2.PageIndexChanged
DataGrid2.CurrentPageIndex = e.NewPageIndex
bindgrid2()
End Sub
End Class
Output