zoukankan
html css js c++ java
在datagrid中求和(vb.net,c#)
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
Namespace
myApp
Public
Class calcTotals
Class
calcTotals :
Inherits
Page
Protected
MyGrid
As
DataGrid
Private
runningTotal
As
double
=
0
'
定义合计变量
Private
Sub CalcTotal()
Sub
CalcTotal(_price
As
String
)
'
求和
Try
runningTotal
+=
Double
.Parse(_price)
Catch
'
空值
End
Try
End Sub
Public
Sub MyDataGrid_ItemDataBound()
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()
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());
}
}
}
}
查看全文
相关阅读:
PostgreSQL数据库管理:定期vacuum
关于压力测试的思路
作业自动提示功能设计思路
This system is not registered with RHN
读《高性能建站指南》(上)
Ubuntu下PostgreSQL数据库集群(PL/Proxy)配置方法
PG SQL数据库读写分离的思路
ASP.NET知识点的明晰(非原创,东拼西凑的,向这些内容的原创者致敬)
[转]Effective C#原则4:用条件属性而不是#if
如何在Word文档中插入一条分隔线?
原文地址:https://www.cnblogs.com/huang/p/318176.html
最新文章
arcgis 放大到某个比例尺后BMP或者EMF图片符号填充效果消失
PostgresSQL10 主从复制配置
美军称五角大楼遭网络袭击 2.4万份敏感文件被盗 狼人:
黑客组织声称入侵NATO服务器 狼人:
黑客组织Anonymous发布9万个军方密码信息 狼人:
公有云和私有云谁更安全? 狼人:
微软称任何僵尸网络都可以被瓦解 狼人:
应用权限Android面试题总结(六)
网关输入mni2440烧写裸机程序(Uboot下载)
单片机输出C#点灯神话
热门文章
百度谷歌2013年母亲节 赏析中文搜索引擎庆祝涂鸦
定义控件android学习笔记50_样式与主题,给控件使用样式,给应用使用主题
培训工资我看大学培训机构大学生到底要不要参加培训机构 一个参加培训的大学生的真实感受
网络应用程序Web3.0概述
应用返回第五章(3)Libgdx应用框架之查询
注意错误zoj2110Tempter of the Bone
存储过程程序MYSQL存储过程编程2
主题:Centos6.4安装JDK
.NET Framework 4.0 SDK的安装
Postgres与OS内核相关的几个参数设置
Copyright © 2011-2022 走看看