<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<title>Binding a DataReader to a GridView Control</title>
<link rel="stylesheet" href="/shared/netdemos.css">
<script language="C#" runat="server" src="~/shared/fetchData_sql.cs" />
<script language="C#" runat="server">
protected void Page_Load ( object src, EventArgs e ) {
if ( !IsPostBack ) {
// define the query
string query = "SELECT * FROM Books WHERE Subject = 'C#' AND Price IS NOT NULL";
// fetch results of the query into a datareader
myGrid.DataSource = fetchReader ( query );
// bind the gridview to the reader
myGrid.DataBind ( );
}
}
</script>
<body>
<!-- #include virtual="~/shared/top.inc -->
<div class="header"><h2>Binding a <b>DataReader</b> to a <b>GridView</b> Control</h2></div>
<!-- #include virtual="~/shared/viewsrc_top.inc" -->
<hr size=1 width=92%>
<div align="center">
<form runat="server">
<asp:gridview id="myGrid" runat="server"
autogeneratecolumns=false
width="90%" cellpadding=5 cellspacing=0
backcolor="ghostwhite"
font-size="10pt"
enableviewstate=false>
<headerstyle height=30 font-bold
backcolor="steelblue"
forecolor="beige" />
<columns>
<asp:imagefield headertext=""
dataimageurlfield="titleid"
dataimageurlformatstring="/shared/books/sm/{0}.jpg" />
<asp:hyperlinkfield headertext="Title"
datatextfield="title" target="amzn"
datanavigateurlfields="url" />
<asp:boundfield headertext="Author"
datafield="Author" />
<asp:boundfield headertext="Publisher"
datafield="Publisher" />
<asp:boundfield headertext="Price (from)"
datafield="Price"
dataformatstring="{0:c}"
itemstyle-horizontalalign="right" />
</columns>
</asp:gridview>
</form>
</div>
<hr size=1 width=92%>
<!-- #include virtual="~/shared/viewsrc.inc" -->
</body>
</html>