ASP.NET数据格中计算数值总和
以表格形式显示数据可以带来很多好处。在本文中,我将讲解如何使用datagrid计算总计,这在处理数值时会经常用到。
在讨论datagrid控制时,常常可以听到别人对此方法的嘲笑。他们常常抛弃它转而使用第三方的工具。事实上,datagrid作为. net framework的核心部分,已成为我开发工具箱中极具价值的工具。
什么是总计?
在应用程序中使用datagrid控制可以允许你以对绝大部分用户来说熟悉的格式来发布数据(栅格格式常常被用于如微软excel等电子数据表格应用程序)。使用此类型的应用程序,用户可以按照习惯查看自定义函数如每栏总计、平均值等。而这些函数并不是datagrid的标准函数,你可以自行编写代码来轻松地实现这些函数。
在本例中,我将使用所有sql server版本都可提供的northwind范例数据库,并从顺序表格中提取数据。我将对货物栏计算总计值,这个总计值应当在datagrid中一致地显示出来。一下就是此应用程序的c#代码。
<%@ import namespace="system.data.sqlclient" %>
<%@ import namespace="system.data" %>
<%@ page language="c#" %>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en" >
<html><head><title>builder.com datagrid totals example</title>
</head>
<body ms_positioning="gridlayout">
double totalfreight = 0;
private void page_load(object sender, system.eventargs e) {
if (!page.ispostback) {
binddata();
} }
private void binddata() {
const string sconn;
sconn = "server=(local);initial catalog=northwind;uid=ctester;pwd=password";
try {
sqlconnection conn = new sqlconnection(sconn);
conn.open();
string ssql = "select top 10 orderid, freight, shipname, shipcountry from
orders";
sqlcommand comm = new sqlcommand(ssql, conn);
sqldatareader dr = comm.executereader();
dgnorthwind.datasource = dr;
dgnorthwind.databind();
} catch (exception e) {
console.writeline(e.tostring());
} }
private void dototal(object sender, datagriditemeventargs e) {
if (e.item.itemtype == listitemtype.item | e.item.itemtype ==
listitemtype.alternatingitem) {
double currentfreight = convert.todouble(databinder._eval(e.item.dataitem,
"freight"));
totalfreight += currentfreight;
} else if (e.item.itemtype == listitemtype.footer) {
e.item.cells[2].text = "total:";
e.item.cells[3].text = convert.tostring(totalfreight);
} }
</script>
<form id="frmdatagridtotals" method="post" runat="server">
<asp:datagrid id="dgnorthwind"
style="z-index: 101; left: 24px; position: absolute; top: 32px"
runat="server" height="320px" width="496px"
autogeneratecolumns="false"
onfiltered="dototal"
showfooter="true" cellpadding="4" cellspacing="0"
borderstyle="solid" borderwidth="1" gridlines="none"
bordercolor="black"
itemstyle-font-name="verdana"
itemstyle-font-size="9pt"
headerstyle-font-name="verdana"
headerstyle-font-size="10pt"
headerstyle-font-bold="true"
headerstyle-forecolor="white"
[来源 www.iocblog.net]
headerstyle-backcolor="gray"
footerstyle-font-name="verdana"
footerstyle-font-size="10pt"
footerstyle-font-bold="true"
footerstyle-forecolor="red"
footerstyle-backcolor="gray">
<columns>
<asp:boundcolumn datafield="orderid" headertext="#" itemstyle-width="10%"
headerstyle-horizontalalign="center" />
<asp:boundcolumn datafield="shipname" headertext="customer" itemstyle
-width="50%" />
<asp:boundcolumn datafield="shipcountry" headertext="country" itemstyle
-width="20%" />
<asp:boundcolumn datafield="freight" headertext="freight" itemstyle-width="20%"
/>
</columns></asp:datagrid>
</form></body></html>
或许首先你注意到的是此页面没有使用code-behind特性。所有的代码都放在aspx文件中。页面首部必需的import指令保证了数据交互所需代码的可用性。页面事件page_load调用了binddata方法,这正是与数据库交互的地方。它连接到数据库并创建sqldatareader对象,此对象包含了由sql语句返回的记录。对象sqldatareader通过对象datagrid的datasource属性把datagrid对象放入页面中。对象datagrid的databind方法负责装入数据。datagrid的html定义了栏目及其格式,包括颜色、字体、对齐等。[来源 www.iocblog.net]
文章整理:iocblog
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。