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

<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 728x15, 创建于 08-4-23MSDN */ google_ad_slot = "3624277373"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 160x600, 创建于 08-4-23MSDN */ google_ad_slot = "4367022601"; google_ad_width = 160; google_ad_height = 600; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
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());
      }
    }
 &nb
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 728x15, 创建于 08-4-23MSDN */ google_ad_slot = "3624277373"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 160x600, 创建于 08-4-23MSDN */ google_ad_slot = "4367022601"; google_ad_width = 160; google_ad_height = 600; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
阅读更多
个人分类: .net
想对作者说点什么? 我来说一句

DataGridView添加Button按钮

2016年10月26日 86KB 下载

datagridview自动求和

2009年02月24日 29KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭