- 热门文章:
- · ASP.NET中利用SQLXML WEB服务访问XML数据(转)
- · 在DataGrid中经弹出窗口确认后执行操作(删除)。(附在DataGrid中修改,添加记录)详见代码!!
- · 在datagrid中放入一个DropDownList(忘了这个问题在那里看到的了CSDN or There)
- · 简单的动态加载用户控件的方法
- · 关于如何 确认删除的另外一个办法。
- · 掉掉注意了,如何动态加载用户控件(ascx)
- · 给大家一个新的加密方法,C#的。(国外的,只用于学习,支持中文)
- · TO feixr,DataGrid中的Radiobutton
- · 微软.NET战略和ASP.NET简介(1)
- · 微软.NET战略和ASP.NET简介(3)
- · 微软.NET战略和ASP.NET简介(2)
- · 验证控件介绍--RegularExpressionValidator
上一篇:一次同时上传多个文件 >>
在datagrid中求和(vb.net,c#)
<%@ 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());
}
}
}
}
相关文章:
- · 验证控件介绍--RangeValidator
- · 验证控件介绍--CompareValidator
- · 验证控件介绍--RequiredFieldValidator
- · 解决分页的例子。使用DataSet绑定到DataList实现的。数据库使用我刚才贴的这个。
- · 配置Config.web
- · WebRequest Class
- · asp.net中使用静态变量
- · 利用HttpRequest登录到某个网站,然后获取网站信息的程序示例 [原创]
- · Creating DataGrid Templated Columns Dynamically - Part II(转自DotNetTips)
- · 用ASP.NET写你自己的代码生成器(2)。
- · 用ASP.NET写你自己的代码生成器(3)。
- · mark新官上任,转贴一个DataGrid(增加删除确认和新增记录功能),道贺:)
- · 一个datagrid 删除确认例子
- · 关于ASP.Net不能启动调试的官方解答
- · 在WEB窗体中如何转换页面和结束程序?
- · 关于ASP.Net写注册表权限问题的官方解决方法
- · 动态生成柱状图
- · 一个SDK里做聊天室的例子(2)
- · 网上下载和上传数据(一) Montaque(原作)
- · 一个SDK里做聊天室的例子(1)
- · 网上下载和上传数据(二) Montaque(原作)
- · 有空的时候看看,:)ASP.NET Page Templates
- · VB.NET开发互联网应用
- · vb.net cookie操作
- · Net中如何操作IIS(原理篇)
- · 关于选用何种ASP.NET设计方法的技巧
- · .Net中如何操作IIS(源代码) (原创)
- · iis 坏掉了,重新安装了以后.netframework 不能用了的解决方法
- · 两个aspx页面间传递引用对象。
- · 在Webcontrol的Toolbar上加入删除确认的方法(改进后)
- · TreeView 派生类: TreeViewEx 实现 NodeShowToolTip、NodeDoubleClick 事件
- · 我自己写的自定义Web的上传控件
- · 增加判断文字长度,汉字算2个
- · 客户端脚本对中文的验证(javascript)
- · 献丑了,我的asp.net网站开发经验,欢迎参加讨论。
- · 笑望人生,关于IHttpHandler处理图片
- · HTML在线编辑器--服务器控件~~.NET实现~~
- · How to Share Session State Between Classic ASP and ASP.NET(1)
