<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<html>
<title>Binding a GridView Control to a Data Source</title>
<link rel="stylesheet" href="/shared/netdemos.css">
<script language="C#" runat="server" src="fetchData_oledb.cs" />
<script runat="server">
void Page_Load ( Object src, EventArgs e ) {
// need to do this only on initial load
if ( !IsPostBack ) bindGrid ( );
}
void bindGrid ( ) {
string query = "SELECT ProductType, ProductCode, Productname, ProductDescription, UnitPrice FROM Products";
myGrid.DataSource = fetchData ( query, "dbtutor" );
myGrid.DataBind ( );
}
void setPage ( Object src, GridViewPageEventArgs e ) {
// set PageIndex to the page the user clicked.
myGrid.PageIndex = e.NewPageIndex;
// fetch and rebind the data.
bindGrid ( );
}
</script>
<body>
<!-- #include virtual="~/shared/top.inc" -->
<div class="header"><h2>Binding a GridView Control to a Data Source</h2></div>
<hr size=1 width=92%>
<form runat="server">
<div align="center">
<asp:gridview id="myGrid" runat="server"
width=90% cellpadding=5
font-size="9pt"
enableviewstate=false
allowpaging
onPageIndexChanging="setPage">
<headerstyle backcolor="steelblue"
forecolor="beige" />
<rowstyle verticalalign="top" />
</asp:gridview>
</div>
</form>
<hr size=1 width=92%>
<!-- #include virtual="~/shared/viewsrc.inc" -->
</body>
</html>