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());
}
}
}
}
查看全文
相关阅读:
数组元素循环右移
备忘录模式笔记
调度算法(笔记)
meta http-equiv的用法(转)
常用Maven插件介绍(下)(转)
常用Maven插件介绍(上)(转)
maven命令
JDK并发包
线程基础
Jav堆排序
原文地址:https://www.cnblogs.com/huang/p/318176.html
最新文章
post get 的区别
【转】WebForm页面生命周期及asp.net运行机制复习
Python第三课——数据类型与运算(2)
道歉
Python第三课——数据类型与运算(1)
Python第二课——变量和Python编译器的使用
Python第一课——输入和输出和编译器下载
写在前面:
洛谷 P1089 【津津的储蓄计划】
01背包
热门文章
用高精度实现求完满数的方法
从应用层打通内核:驱动的工作原理
linux 设备树中 dwc3 节点的phys参数含义
const char* to char*(当函数传递参数时)
安装opencv3.3.0碰到的问题及解决方法
wps字体缺失,问题
kean的博客今天开通了,happy 一下
1011. A+B和C (15)
大数的减法函数--c语言
大数的加法函数--c语言
Copyright © 2011-2022 走看看