<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<head>
<title>DataGrid ItemDataBound Event Example</title>
<link rel="stylesheet" href="/shared/netdemos.css">
<script language="C#" runat="server" src="fetchData_sql.cs" />
<script language="C#" 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 ( );
getItems ( null, null );
}
}
void getItems ( Object src, EventArgs e ) {
string query = "SELECT * FROM [Orders Summary] WHERE CustomerID = '" +
lstCustomers.SelectedValue + "' ORDER BY OrderID";
dgOrders.DataSource = fetchReader ( query );
dgOrders.DataBind ( );
}
public void sumItems ( Object src, DataGridItemEventArgs e ) {
if ( e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem ) {
TableCellCollection myRow = e.Item.Cells;
// assumes column to sum is last column in grid
total += float.Parse ( myRow [ myRow.Count-1 ].Text );
myRow [ myRow.Count-1 ].Text = string.Format ( "{0:n}",
float.Parse ( myRow [ myRow.Count-1 ].Text ) );
}
}
public void setFooter ( Object src, DataGridItemEventArgs e ) {
if ( e.Item.ItemType == ListItemType.Footer ) {
TableCellCollection myRow = e.Item.Cells;
myRow.RemoveAt ( 0 ); myRow.RemoveAt ( 0 );
myRow [ 0 ].ColumnSpan = 3;
myRow [ 0 ].Text = lstCustomers.SelectedItem.Text;
myRow [ 1 ].Text = "Total";
myRow [ 2 ].Text = string.Format ( "{0:c}", total );
myRow [ 2 ].BackColor = System.Drawing.Color.Maroon;
myRow [ 2 ].HorizontalAlign = HorizontalAlign.Right;
}
}
</script>
</head>
<body>
<!-- #include virtual="~/shared/top.inc" -->
<div class="header"><h2>DataGrid ItemDataBound Event 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="getItems" /></p>
<asp:datagrid id="dgOrders" runat="server"
width=90% cellpadding=5 font-size="10pt"
gridlines="horizontal" showfooter
autogeneratecolumns=false
onItemDataBound="sumItems"
onItemCreated="setFooter">
<headerstyle backcolor="darkslategray"
forecolor="khaki" font-bold />
<itemstyle backcolor="ghostwhite" verticalalign="top" />
<footerstyle backcolor="steelblue"
forecolor="snow" font-bold />
<columns>
<asp:boundcolumn headertext="Invoice"
datafield="orderid" />
<asp:boundcolumn headertext="OrderDate"
datafield="OrderDate"
dataformatstring="{0:d}" />
<asp:boundcolumn headertext="ShippedDate"
datafield="ShippedDate"
dataformatstring="{0:d}" />
<asp:boundcolumn headertext="SalesPerson"
datafield="SalesPerson" />
<asp:boundcolumn headertext="SaleAmount"
datafield="SaleAmount"
dataformatstring="{0:n}"
itemstyle-horizontalalign="right" />
</columns>
</asp:datagrid>
</center>
</form>
<hr size=1 width=92%>
<!-- #include virtual="~/shared/viewsrc.inc" -->
</body>
</html>