时间:2021-07-01 10:21:17 帮助过:19人阅读
<%@ Page Inherits="myApp.calcTotals" Src="20010731T0101.aspx.cs" %> 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" /> |
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; } } |
protected void Page_Load(object sender, EventArgs e) { SqlConnection myConnection = new SqlConnection("server=Localhost;database=pubs;uid=sa;pwd=;");//创建SQL连接 SqlCommand myCommand = new SqlCommand("SELECT title, price FROM Titles WHERE price > 0", myConnection);//创建SQL命令 try { myConnection.Open();//打开数据库连接 MyGrid.DataSource = myCommand.ExecuteReader();//指定 DataGrid 的数据源 MyGrid.DataBind();//绑定数据到 DataGrid myConnection.Close();//关闭数据连接 } catch(Exception ex) { //捕获错误 HttpContext.Current.Response.Write(ex.ToString()); } } |
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); } } |