在datagrid中求和(vb.net,c#)

aspx文件




ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif @ Page Inherits="myApp.calcTotals" Src=""  %>
None.gif
<!-- 自己改一下src -->
None.gif
< html >
None.gif
< body  bgcolor ="white" >
None.gif
< asp:DataGrid  id ="MyGrid"  runat ="server"
None.gif  AutoGenerateColumns
="False"
None.gif  CellPadding
="4"  CellSpacing ="0"
None.gif  BorderStyle
="Solid"  BorderWidth ="1"
None.gif  Gridlines
="None"  BorderColor ="Black"
None.gif  ItemStyle-Font-Name
="Verdana"
None.gif  ItemStyle-Font-Size
="9pt"
None.gif  HeaderStyle-Font-Name
="Verdana"
None.gif  HeaderStyle-Font-Size
="10pt"
None.gif  HeaderStyle-Font-Bold
="True"
None.gif  HeaderStyle-ForeColor
="White"
None.gif  HeaderStyle-BackColor
="Blue"
None.gif  FooterStyle-Font-Name
="Verdana"
None.gif  FooterStyle-Font-Size
="10pt"
None.gif  FooterStyle-Font-Bold
="True"
None.gif  FooterStyle-ForeColor
="White"
None.gif  FooterStyle-BackColor
="Blue"
None.gif  OnItemDataBound
="MyDataGrid_ItemDataBound"
None.gif  ShowFooter
="True" >
None.gif
<!-- 在footer中显示合计 -->
None.gif  
< Columns >
None.gif    
< asp:BoundColumn  HeaderText ="Title"  DataField ="title"   />
None.gif    
< asp:BoundColumn  HeaderText ="Price"  DataField ="price"
None.gif      ItemStyle-HorizontalAlign
="Right"
None.gif      HeaderStyle-HorizontalAlign
="Center"   />
None.gif  
</ Columns >
None.gif
</ asp:DataGrid >
None.gif
</ body >
None.gif
</ html >
下面给出vb.net和C#两种代码
vb.net
None.gif Imports  System
None.gif
Imports  System.Web
None.gif
Imports  System.Web.UI
None.gif
Imports  System.Web.UI.WebControls
None.gif
Imports  System.Web.UI.HtmlControls
None.gif
Imports  System.Data
None.gif
Imports  System.Data.SqlClient
None.gif
ExpandedBlockStart.gifContractedBlock.gif
Namespace myApp Namespace myApp
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
Public Class calcTotalsClass calcTotals : Inherits Page
InBlock.gif  
InBlock.gif    
Protected MyGrid As DataGrid
InBlock.gif    
Private runningTotal As double = 0
InBlock.gif
InBlock.gif
'定义合计变量
ExpandedSubBlockStart.gifContractedSubBlock.gif
    Private Sub CalcTotal()Sub CalcTotal(_price As String)
InBlock.gif
'求和
InBlock.gif
      Try
InBlock.gif        runningTotal 
+= Double.Parse(_price)
InBlock.gif      
Catch
InBlock.gif        
' 空值
InBlock.gif
      End Try
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Sub MyDataGrid_ItemDataBound()Sub MyDataGrid_ItemDataBound(sender As Object, e As DataGridItemEventArgs)
InBlock.gif
InBlock.gif      
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
InBlock.gif
InBlock.gif        CalcTotal( e.Item.Cells(
1).Text )
InBlock.gif
'循环执行求和程序
InBlock.gif
        e.Item.Cells(1).Text = string.Format("{0:c}", Convert.ToDouble(e.Item.Cells(1).Text))
InBlock.gif
InBlock.gif      
Elseif(e.Item.ItemType = ListItemType.Footer )
InBlock.gif
InBlock.gif        e.Item.Cells(
0).Text="Total"
InBlock.gif        e.Item.Cells(
1).Text = string.Format("{0:c}", runningTotal)
InBlock.gif      
End If
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Protected Sub Page_Load()Sub Page_Load(sender As object, e As EventArgs)
InBlock.gif    
InBlock.gif      
Dim myConnection As New SqlConnection("server=Localhost;database=pubs;uid=sa;pwd=")
InBlock.gif      
Dim myCommand As New SqlCommand("SELECT title, price FROM Titles WHERE price > 0", myConnection)
InBlock.gif      
InBlock.gif      
Try
InBlock.gif
InBlock.gif        myConnection.Open()
InBlock.gif        MyGrid.DataSource 
= myCommand.ExecuteReader()
InBlock.gif        MyGrid.DataBind()
InBlock.gif        myConnection.Close()
InBlock.gif
InBlock.gif      
Catch ex As Exception
InBlock.gif
InBlock.gif        
' 有错误发生
InBlock.gif
        HttpContext.Current.Response.Write(ex.ToString())
InBlock.gif      
End Try
ExpandedSubBlockEnd.gif    
End Sub

ExpandedSubBlockEnd.gif  
End Class

ExpandedBlockEnd.gif
End Namespace

C#道理和vb.net是一样的就多做解释了
None.gif using  System;
None.gif
using  System.Web;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
using  System.Data;
None.gif
using  System.Data.SqlClient;
None.gif
None.gif
namespace  myApp
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif  
public class calcTotals : Page
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif    
protected DataGrid MyGrid;
InBlock.gif    
private double runningTotal = 0;
InBlock.gif
InBlock.gif    
private void CalcTotal(string _price)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
try
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        runningTotal 
+= Double.Parse(_price);
ExpandedSubBlockEnd.gif      }

InBlock.gif      
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{      
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public void MyDataGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        CalcTotal( e.Item.Cells[
1].Text );
InBlock.gif        e.Item.Cells[
1].Text = string.Format("{0:c}", Convert.ToDouble(e.Item.Cells[1].Text));
ExpandedSubBlockEnd.gif      }

InBlock.gif      
else if(e.Item.ItemType == ListItemType.Footer )
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        e.Item.Cells[
0].Text="Total";
InBlock.gif        e.Item.Cells[
1].Text = string.Format("{0:c}", runningTotal);
ExpandedSubBlockEnd.gif      }
  
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      SqlConnection myConnection 
= new SqlConnection("server=Localhost;database=pubs;uid=sa;pwd=;");
InBlock.gif      SqlCommand myCommand 
= new SqlCommand("SELECT title, price FROM Titles WHERE price > 0", myConnection);
InBlock.gif      
InBlock.gif      
try
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        myConnection.Open();
InBlock.gif        MyGrid.DataSource 
= myCommand.ExecuteReader();
InBlock.gif        MyGrid.DataBind();
InBlock.gif        myConnection.Close();
ExpandedSubBlockEnd.gif      }

InBlock.gif      
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        HttpContext.Current.Response.Write(ex.ToString());
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif}


转载于:https://www.cnblogs.com/huang/archive/2006/01/16/318176.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值