最近看到HashTable的msdn範例..小弟做一個簡單的GridView的小範例..
介紹如何使用HashTable來計算一些值,感覺還不錯用..分享給大家呀..
asp.net(c#)
TestGV2.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestGV2.aspx.cs" Inherits="TestGV2" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>GridViewHashtable</title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" CellPadding="2" DataKeyNames="id" DataSourceID="SqlDataSource1" ForeColor="Black" GridLines="None" OnRowDataBound="GridView1_RowDataBound" ShowFooter="True" Width="300px"> <Columns> <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True" SortExpression="id"> <HeaderStyle HorizontalAlign="Left" /> </asp:BoundField> <asp:BoundField DataField="name" HeaderText="name" SortExpression="name"> <HeaderStyle HorizontalAlign="Left" /> </asp:BoundField> <asp:BoundField DataField="money" HeaderText="money" SortExpression="money"> <HeaderStyle HorizontalAlign="Left" /> </asp:BoundField> </Columns> <FooterStyle BackColor="Tan" /> <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" /> <HeaderStyle BackColor="Tan" Font-Bold="True" /> <AlternatingRowStyle BackColor="PaleGoldenrod" /> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [Table1]"></asp:SqlDataSource> </div> </form> </body> </html>
TestGV2.aspx.cs
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Collections.Generic; public partial class TestGV2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { Hashtable ht = new Hashtable(); string name = ""; int money = 0; foreach (GridViewRow item in this.GridView1.Rows) { name = item.Cells[1].Text; money = Convert.ToInt32(item.Cells[2].Text); if (ht.ContainsKey(name)) { ht[name] = Convert.ToInt32(ht[name]) + money; } else { ht.Add(name, money); } } //在GridView的Footer加入count與sum資訊 if (e.Row.RowType == DataControlRowType.Footer) { int count = 0;//計算name的個數 int sum = 0;//計算所有name的money的總合 //Hashtable的foreach使用DictionaryEntry foreach (DictionaryEntry item in ht) { count++; sum += Convert.ToInt32(item.Value); } //顯示name的個數 e.Row.Cells[1].Text = "count:" + count; //顯示所有name的money總合 e.Row.Cells[2].Text = "sum:" + sum; } } }
執行結果:
參考網址:
http://msdn.microsoft.com/zh-tw/library/system.collections.hashtable(VS.80).aspx