在datagrid中求和(vb.net,c#)
2024-07-10 12:58:27
供稿:网友
aspx文件
<%@ page inherits="myapp.calctotals" src="" %>
<!--自己改一下src-->
<html>
<body bgcolor="white">
<asp:datagrid id="mygrid" runat="server"
autogeneratecolumns="false"
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"
headerstyle-backcolor="blue"
footerstyle-font-name="verdana"
footerstyle-font-size="10pt"
footerstyle-font-bold="true"
footerstyle-forecolor="white"
footerstyle-backcolor="blue"
onitemdatabound="mydatagrid_itemdatabound"
showfooter="true">
<!--在footer中显示合计-->
<columns>
<asp:boundcolumn headertext="title" datafield="title" />
<asp:boundcolumn headertext="price" datafield="price"
itemstyle-horizontalalign="right"
headerstyle-horizontalalign="center" />
</columns>
</asp:datagrid>
</body>
</html>
下面给出vb.net和c#两种代码
vb.net
imports system
imports system.web
imports system.web.ui
imports system.web.ui.webcontrols
imports system.web.ui.htmlcontrols
imports system.data
imports system.data.sqlclient
namespace myapp
public class calctotals : inherits page
protected mygrid as datagrid
private runningtotal as double = 0
'定义合计变量
private sub calctotal(_price as string)
'求和
try
runningtotal += double.parse(_price)
catch
' 空值
end try
end sub
public sub mydatagrid_itemdatabound(sender as object, e as datagriditemeventargs)
if e.item.itemtype = listitemtype.item or e.item.itemtype = listitemtype.alternatingitem then
calctotal( e.item.cells(1).text )
'循环执行求和程序
e.item.cells(1).text = string.format("{0:c}", convert.todouble(e.item.cells(1).text))
elseif(e.item.itemtype = listitemtype.footer )
e.item.cells(0).text="total"
e.item.cells(1).text = string.format("{0:c}", runningtotal)
end if
end sub
protected sub page_load(sender as object, e as eventargs)
dim myconnection as new sqlconnection("server=localhost;database=pubs;uid=sa;pwd=")
dim mycommand as new sqlcommand("select title, price from titles where price > 0", myconnection)
try
myconnection.open()
mygrid.datasource = mycommand.executereader()
mygrid.databind()
myconnection.close()
catch ex as exception
' 有错误发生
httpcontext.current.response.write(ex.tostring())
end try
end sub
end class
end namespace
c#道理和vb.net是一样的就多做解释了
using system;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
using system.data;
using system.data.sqlclient;
namespace myapp
{
public class calctotals : page
{
protected datagrid mygrid;
private double runningtotal = 0;
private void calctotal(string _price)
{
try
{
runningtotal += double.parse(_price);
}
catch
{
}
}
public void mydatagrid_itemdatabound(object sender, datagriditemeventargs e)
{
if (e.item.itemtype == listitemtype.item || e.item.itemtype == listitemtype.alternatingitem)
{
calctotal( e.item.cells[1].text );
e.item.cells[1].text = string.format("{0:c}", convert.todouble(e.item.cells[1].text));
}
else if(e.item.itemtype == listitemtype.footer )
{
e.item.cells[0].text="total";
e.item.cells[1].text = string.format("{0:c}", runningtotal);
}
}
protected void page_load(object sender, eventargs e)
{
sqlconnection myconnection = new sqlconnection("server=localhost;database=pubs;uid=sa;pwd=;");
sqlcommand mycommand = new sqlcommand("select title, price from titles where price > 0", myconnection);
try
{
myconnection.open();
mygrid.datasource = mycommand.executereader();
mygrid.databind();
myconnection.close();
}
catch(exception ex)
{
httpcontext.current.response.write(ex.tostring());
}
}
}
}