Paging with a DataGrid

Just copy and paste this code into a new blank page. Change the Database name, UID and password. It uses the Northwind database with SQL Server and save it as a ‘.aspx’ page.

To make it “Next-Previous” instead of numbering – change the PagerStyle-Mode to:
PagerStyle-Mode=’NextPrev’
PagerStyle-NextPageText=’Next ->’
PagerStyle-PrevPageText=’<- Previous'
Just copy and paste this code into a new blank page. Change the Database name, UID and password. It uses the Northwind database with SQL Server and save it as a ‘.aspx’ page.

To make it “”Next-Previous”" instead of numbering – change the PagerStyle-Mode to:

PagerStyle-Mode=’NextPrev’
PagerStyle-NextPageText=’Next ->’
PagerStyle-PrevPageText=’<- Previous’

<%@ Import Namespace=”System.Data” %>
<%@ Import Namespace=”System.Data.SqlClient” %>
<script language=”VB” runat=”server”>

Sub Page_Load(sender As Object, e As EventArgs)
BindGrid
End Sub

Sub Page_Change(sender As Object, e As DataGridPageChangedEventArgs)
dim start as Integer
start = MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize
MyDataGrid.CurrentPageIndex = e.NewPageIndex
BindGrid
End Sub

Sub BindGrid()
‘You can store the connection as a string in Web.Config, if you want:
‘Dim myConnection As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings(“Whatever”))

Dim myConnection As SqlConnection = New SqlConnection(“server=YourServer;uid=UserName;pwd=Password;database=northwind”)

dim ds as DataSet = new DataSet()
dim adapter as SqlDataAdapter = new SqlDataAdapter(“Select * from customers”, myConnection)
adapter.Fill(ds,”customers”)

MyDataGrid.DataSource=ds.Tables(“customers”).DefaultView
MyDataGrid.DataBind()
End Sub

Sub PagerButtonClick(sender As Object, e As EventArgs)
BindGrid
End Sub

</script>
<html>
<head>
</head>
<body>
<div align=”center”>Paging with ASP.Net</div>

<form runat=”server”>

<ASP:DataGrid id=”MyDataGrid” runat=”server”
AllowPaging=”True”
PageSize=”5″
PageCount=”1″
PagerStyle-Mode=”NumericPages”
PagerStyle-HorizontalAlign=”Center”
OnPageIndexChanged=”Page_Change”
BorderColor=”black”
BorderWidth=”1″
GridLines=”Both”
CellPadding=”3″
CellSpacing=”0″
Font-Name=”Verdana”
Font-Size=”8pt”
HeaderStyle-BackColor=”#aaaadd”
AlternatingItemStyle-BackColor=”#eeeeee”
width=”100%”
/>
<p>
</form>

</body>
</html>

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>