NewsRhzhi | 先创资讯 | 旧版入口
rhzhi.net
网站首页 | NewsRhzhi | 先创资讯 | 操作系统 | 工具软件 | 办公软件 | 网站设计 | 组网专栏 | 平面设计 | 多 媒 体 | 程序开发 | 硬件资料 | 聊天软件
您现在的位置: 先创网 >> 程序开发 >> .NET >> 文章正文
在datagrid中求和(vb.net,c#)
不详
2005-3-14文/佚名
    

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());
      }
    }
  }
}

打印此页 投稿与建议 返回顶部
栏 目 索 引
软件应用 SOFTWARE
Win XP | NT/2003
Win2000 | DOS/Win9x
PowerPoint | Office
Excel | Word
网络软件 | 实用软件
媒体软件 | 系统软件
常用软件 | 办公软件
聊天软件 | 网络安全
新软试用 | Vista
设计在线 DESIGN
Dreamweaver | 3DMax
Photoshop | Flash
平面设计 | 网页设计
多 媒 体 | 精品画廊
精彩专区 SPECIAL
Q Q 专区 | 热门专题
组网玩网 | 程序开发
应用集锦 |

没有任何图片文章
相关文章
关于我们 - 联系方式 - 合作伙伴 - 网站大事记 - 网站地图 - 我要投稿
Copyright ©1997-2008 先创网 All Rights Reserved.
先创科技 版权所有