All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
ASP.NET2.0站点跨服务器访问Sql,Sever,2005,Reporting,Service_[Asp.Net教程]
PHP5连接Mysql5的认证协议问题_[PHP教程]
学习动态网页技术PHP:GD库安装问题详解_php资料_编程技术
AJAX并不神秘:揭密各种AJAX控件和类库_[AJAX教程]
Asp.Net中NHiernate的Session的管理_[Asp.Net教程]
Flex+asp.net上传文件_[Asp.Net教程]
C#文件处理技术:FileStream使用方法
技巧应用:ASP.Net中的MD5加密_.net资料_编程技术
Asp.Net,网站项目打包_[Asp.Net教程]
RSS制作(C#):使用XML,DOM_[Asp.Net教程]
Reading number is top 10 pictures
Absolutely shocked. National geographic 50 animal photographys3
So beauty, will let you spray blood9
The Soviet union swimsuit exposure in the 70 year1
China's zhejiang university to create the world's most light material
In the world the most mysterious 21 place landscape4
Summer is most suitable for young people to travel in China9
NeedWallpaper10
在加油站厕所门口看到的告示
Park waits to have her picture taken exposed
美女浴室写真1
Download software ranking
I for your crazy
Tram sex maniac 2 (H) rar bag5
WebService在.NET中的实战应用教学视频 → 第2集
I'm come from Beijing1
塘西风月痕
Call Of Duty2
Kung.Fu.Panda.2
linux初级教程
软件工程思想
电车之狼R
归海一刀 published in(发表于) 2014/1/30 1:32:37 Edit(编辑)
在datagrid中求和的代码(c#、vb.net)_[Asp.Net教程]

在datagrid中求和的代码(c#、vb.net)_[Asp.Net教程]

在datagrid中求和的代码(c#、vb.net)_[Asp.Net教程]
aspx文件
<%@ Page Inherits="myApp.calcTotals" Src="" %>




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">




ItemStyle-HorizontalAlign="Right"
HeaderStyle-HorizontalAlign="Center" />




下面给出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());
}
}



添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.