欧美性猛交黑人xxxx,成人毛片一区二区三区,久久综合九色综合精品,男的把j放进女人下面视频免费

ASP.NET數(shù)據(jù)格中計算數(shù)值總和

  • 發(fā)布于:2024-02-11
  • 185 人圍觀

以表格形式顯示數(shù)據(jù)可以帶來很多好處。在本文中,我將講解如何使用DataGrid計算總計,這在處理數(shù)值時會經(jīng)常用到。

在討論DataGrid控制時,常??梢月牭絼e人對此方法的嘲笑。他們常常拋棄它轉(zhuǎn)而使用第三方的工具。事實上,DataGrid作為. NET Framework的核心部分,已成為我開發(fā)工具箱中極具價值的工具。

什么是總計?

在應(yīng)用程序中使用DataGrid控制可以允許你以對絕大部分用戶來說熟悉的格式來發(fā)布數(shù)據(jù)(柵格格式常常被用于如微軟Excel等電子數(shù)據(jù)表格應(yīng)用程序)。使用此類型的應(yīng)用程序,用戶可以按照習(xí)慣查看自定義函數(shù)如每欄總計、平均值等。而這些函數(shù)并不是DataGrid的標準函數(shù),你可以自行編寫代碼來輕松地實現(xiàn)這些函數(shù)。

在本例中,我將使用所有SQL Server版本都可提供的Northwind范例數(shù)據(jù)庫,并從順序表格中提取數(shù)據(jù)。我將對貨物欄計算總計值,這個總計值應(yīng)當在DataGrid中一致地顯示出來。一下就是此應(yīng)用程序的C#代碼。

<%@ Import Namespace="System.Data.SqlClient" %>

<%@ Import Namespace="System.Data" %>

<%@ Page language="c#" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

<HTML><HEAD><title>Builder.com DataGrid Totals Example</title>

</HEAD>

<body MS_POSITIONING="GridLayout">

double totalFreight = 0;

private void Page_Load(object sender, System.EventArgs e) {

if (!Page.IsPostBack) {

BindData();

} }

private void BindData() {

const string sConn;

sConn = "server=(local);Initial Catalog=Northwind;UID=ctester;PWD=password";

try {

SqlConnection conn = new SqlConnection(sConn);

conn.Open();

string sSQL = "SELECT TOP 10 OrderID, Freight, ShipName, ShipCountry FROM

 Orders";

SqlCommand comm = new SqlCommand(sSQL, conn);

SqlDataReader dr = comm.ExecuteReader();

dgNorthwind.DataSource = dr;

dgNorthwind.DataBind();

} catch (Exception e) {

Console.WriteLine(e.ToString());

} }

private void doTotal(object sender, DataGridItemEventArgs e) {

if (e.Item.ItemType == ListItemType.Item | e.Item.ItemType ==

 ListItemType.AlternatingItem) {

double currentFreight = Convert.ToDouble(DataBinder._Eval(e.Item.DataItem,

 "Freight"));

totalFreight += currentFreight;

} else if (e.Item.ItemType == ListItemType.Footer) {

e.Item.Cells[2].Text = "Total:";

e.Item.Cells[3].Text = Convert.ToString(totalFreight);

} }

</script>

<form id="frmDataGridTotals" method="post" runat="server">

<asp:DataGrid id="dgNorthwind"

style="Z-INDEX: 101; LEFT: 24px; POSITION: absolute; TOP: 32px"

runat="server" Height="320px" Width="496px"

AutoGenerateColumns="False"

onfiltered="doTotal"

ShowFooter="True" 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="Gray"

FooterStyle-Font-Name="Verdana"

FooterStyle-Font-Size="10pt"

FooterStyle-Font-Bold="True"

FooterStyle-ForeColor="Red"

FooterStyle-BackColor="Gray">

<Columns>

<asp:BoundColumn DataField="OrderID" HeaderText="#" ItemStyle-Width="10%"

 HeaderStyle-HorizontalAlign="Center" />

<asp:BoundColumn DataField="ShipName" HeaderText="Customer" ItemStyle

-Width="50%" />

<asp:BoundColumn DataField="ShipCountry" HeaderText="Country" ItemStyle

-Width="20%" />

<asp:BoundColumn DataField="Freight" HeaderText="Freight" ItemStyle-Width="20%"

 />

</Columns></asp:DataGrid>

</form></body></HTML>

萬企互聯(lián)
標簽: