<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<head>
<title>GridView RowCreated, RowDataBound Events Example</title>
<link rel="stylesheet" href="/shared/netdemos.css">
<script language="C#" runat="server" src="fetchData_sql.cs" />
<script runat="server">
float total = 0;
void Page_Load ( Object src, EventArgs e ) {
if ( !IsPostBack ) {
string query = "SELECT DISTINCT CustomerId, CompanyName FROM [Orders Summary]";
lstCustomers.DataSource = fetchReader ( query );
lstCustomers.DataBind ( );
getRows ( null, null );
}
}
void getRows ( Object src, EventArgs e ) {
string query = "SELECT * FROM [Orders Summary] WHERE CustomerID = '" +
lstCustomers.SelectedValue + "' ORDER BY OrderID";
gvOrders.DataSource = fetchReader ( query );
gvOrders.DataBind ( );
}
public void sumRows ( Object src, GridViewRowEventArgs e ) {
if ( e.Row.RowType == DataControlRowType.DataRow ) {
TableCellCollection cells = e.Row.Cells;
// assumes column to sum is last column in grid
total += float.Parse ( cells [ cells.Count-1 ].Text );
cells [ cells.Count-1 ].Text = string.Format ( "{0:n}",
float.Parse ( cells [ cells.Count-1 ].Text ) );
}
}
public void setFooter ( Object src, GridViewRowEventArgs e ) {
if ( e.Row.RowType == DataControlRowType.Footer ) {
TableCellCollection cells = e.Row.Cells;
cells.RemoveAt ( 0 ); cells.RemoveAt ( 0 );
cells [ 0 ].ColumnSpan = 3;
cells [ 0 ].Text = lstCustomers.SelectedItem.Text;
cells [ 1 ].Text = "Total";
cells [ 2 ].Text = string.Format ( "{0:c}", total );
cells [ 2 ].BackColor = System.Drawing.Color.Maroon;
cells [ 2 ].HorizontalAlign = HorizontalAlign.Right;
}
}
</script>
</head>
<body>
<!-- #include virtual="~/shared/top.inc" -->
<div class="header"><h2>GridView <span class="hilite">RowCreated, RowDataBound Events </span> Example</h2></div>
<hr size=1 width=92%>
<form runat="server">
<center>
<p>Select Customer:
<asp:dropdownlist id="lstCustomers" runat="server" autopostback
datatextfield="companyname" datavaluefield="customerid"
onSelectedIndexChanged="getRows" /></p>
<asp:gridview id="gvOrders" runat="server"
width=75% cellpadding=5 font-size="9pt"
gridlines="horizontal" showfooter
autogeneratecolumns=false
onRowDataBound="sumRows"
onRowCreated="setFooter">
<headerstyle backcolor="darkslategray"
forecolor="khaki" font-bold />
<rowstyle backcolor="ghostwhite" verticalalign="top" />
<footerstyle backcolor="steelblue"
forecolor="snow" font-bold />
<columns>
<asp:boundfield headertext="Invoice"
datafield="orderid" />
<asp:boundfield headertext="OrderDate"
datafield="OrderDate"
htmlencode=false
dataformatstring="{0:d}" />
<asp:boundfield headertext="ShippedDate"
datafield="ShippedDate"
htmlencode=false
dataformatstring="{0:d}" />
<asp:boundfield headertext="SalesPerson"
datafield="SalesPerson" />
<asp:boundfield headertext="SaleAmount"
datafield="SaleAmount"
htmlencode=false
dataformatstring="{0:n}"
itemstyle-horizontalalign="right" />
</columns>
</asp:gridview>
</center>
</form>
<hr size=1 width=92%>
<!-- #include virtual="~/shared/viewsrc.inc" -->
</body>
</html>