Friday, June 25, 2010

Showing data in a DataGrid server control with paging enabled

<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script runat="server">
</script>
<html><head>
</head>
<body>
<form runat="server">
<asp:DataGrid id="DataGrid1" runat="server" AllowPaging="True"
OnPageIndexChanged="DataGrid1_PageIndexChanged"></asp:DataGrid>
</form>
</body>
</html>

Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs)
If Not Page.IsPostBack Then
BindData()
End If
End Sub
Private Sub BindData()
Dim conn As SqlConnection = New SqlConnection("server=’localhost’;
trusted_connection=true; Database=’Northwind’")
Dim cmd As SqlCommand = New SqlCommand("Select * From Customers", conn)
conn.Open()
Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim ds As New DataSet
da.Fill(ds, "Customers")
DataGrid1.DataSource = ds
DataGrid1.DataBind()
End Sub
Private Sub DataGrid1_PageIndexChanged(ByVal source As Object, _
ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs)
DataGrid1.CurrentPageIndex = e.NewPageIndex
BindData()
End Sub

0 comments:

Post a Comment