瀏覽模式: 普通 | 列表

.Net資源 - 建立可加總的Datagrid

Summary Rows in DataGrid Controls

Summary.cs
// Summary.cs - code-behind file

namespace BWSLib
{
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Text;

    public class MyPage : Page
    {
        // Declare as PUBLIC or PROTECTED members all
        // the controls in the layout
        protected DataGrid grid;
        protected Label lblMsg;
        protected DropDownList ddYears;

        // Page OnLoad
        protected override void OnLoad(EventArgs e)
        {
            if (!IsPostBack)
            {
                // Load data and refresh the view
                DataFromSourceToMemory("MyDataSet");
                UpdateDataView();
            }
        }


        // DataFromSourceToMemory
        private void DataFromSourceToMemory(String strDataSessionName)
        {
            // Gets rows from the data source
            DataSet oDS = PhysicalDataRead();
    
            // Stores it in the session cache
            Session[strDataSessionName] = oDS;
        }

        // PhysicalDataRead
        private DataSet PhysicalDataRead()
        {
            String strCnn = "server=localhost;initial catalog=northwind;uid=sa;";
            SqlConnection conn = new SqlConnection(strCnn);

            // Command text using WITH ROLLUP
            StringBuilder sb = new StringBuilder("");
            sb.Append("SELECT ");
            sb.Append("  CASE GROUPING(o.customerid) WHEN 0 THEN o.customerid ELSE '(Total)' END AS MyCustomerID, ");
            sb.Append("  CASE GROUPING(od.orderid) WHEN 0 THEN od.orderid ELSE -1 END AS MyOrderID, ");
            sb.Append("  SUM(od.quantity*od.unitprice) AS price ");
            sb.Append("FROM Orders o, [Order Details] od ");
            sb.Append("WHERE Year(orderdate) = @TheYear AND od.orderid=o.orderid ");
            sb.Append("GROUP BY o.customerid, od.orderid WITH ROLLUP ");
            sb.Append("ORDER BY o.customerid, price");
            String strCmd = sb.ToString();
            sb = null;

            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = strCmd;
            cmd.Connection = conn;    

            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd;

            // Set the "year" parameter
            SqlParameter p1 = new SqlParameter("@TheYear", SqlDbType.Int);
            p1.Direction = ParameterDirection.Input;
            p1.Value = Convert.ToInt32(ddYears.SelectedItem.Text);
            cmd.Parameters.Add(p1);
    
            // The DataSet contains two tables: Orders and Orders1.
            // The latter is renamed to "OrdersSummary" and the two will be put into
            // relation on the CustomerID field.
            DataSet ds = new DataSet();
            da.Fill(ds, "Orders");

            return ds;
        }

        // Refresh the UI
        private void UpdateDataView()
        {
            // Retrieves the data
            DataSet ds = (DataSet) Session["MyDataSet"];
            DataView dv = ds.Tables["Orders"].DefaultView;

            // Re-bind data
            grid.DataSource = dv;
            grid.DataBind();
        }

        // EVENT HANDLER: ItemCreated            
        public void ItemCreated(Object sender, DataGridItemEventArgs e)
        {
            // Get the newly created item
            ListItemType itemType = e.Item.ItemType;

            ///////////////////////////////////////////////////////////////////
            // ITEM and ALTERNATINGITEM
            if (itemType == ListItemType.Item || itemType == ListItemType.AlternatingItem)
            {
                DataRowView drv = (DataRowView) e.Item.DataItem;
                if (drv != null)
                {
                    // Check here the app-specific way to detect whether the
                    // current row is a summary row

                    if ((int) drv["MyOrderID"] == -1)
                    {
                        // Modify the row layout as needed. In this case,
                        //  + change the background color to white
                        //  + Group the first two cells and display company name and #orders
                        //  + Display the total of orders
                        // Graphical manipulations can be done here. Manipulations that require
                        // data access should be done hooking ItemDataBound. They can be done
                        // in ItemCreated only for templated columns.
                        e.Item.BackColor = Color.White;  
                        e.Item.Font.Bold = true;
                        e.Item.Cells.RemoveAt(1);            // remove the order # cell
                        e.Item.Cells[0].ColumnSpan = 2;        // span the custID cell
                        e.Item.Cells[1].HorizontalAlign = HorizontalAlign.Right;
                    }

                }
            }                    
        }

        // EVENT HANDLER: PageIndexChanged
        public void PageIndexChanged(Object sender, DataGridPageChangedEventArgs e)
        {
            grid.CurrentPageIndex = e.NewPageIndex;
            UpdateDataView();
        }

        // EVENT HANDLER: ItemDataBound
        public void ItemDataBound(Object sender, DataGridItemEventArgs e)
        {
            // Retrieve the data linked through the relation
            // Given the structure of the data ONLY ONE row is retrieved
            DataRowView drv = (DataRowView) e.Item.DataItem;
            if (drv == null)
                return;

            // Check here the app-specific way to detect whether the
            // current row is a summary row
            if ((int) drv["MyOrderID"] == -1)
            {
                if (drv["MyCustomerID"].ToString() == "(Total)")
                {
                    e.Item.BackColor = Color.Yellow;
                    e.Item.Cells[0].Text = "Orders total";
                }
                else
                    e.Item.Cells[0].Text = "Customer subtotal";
            }
        }

        public void OnLoadYear(Object sender, EventArgs e)
        {
            DataFromSourceToMemory("MyDataSet");
            UpdateDataView();
        }
    }
}


Summary.cs
‹%@ Page Language="C#" Inherits="BWSLib.MyPage" Src="Summary.cs" Trace="false" %›

‹html›
‹title›Summary Rows‹/title›
‹style›
  a                {behavior:url(....mouseover.htc);}
  hr            {height:2px;color:black;}
  .StdTextBox    {font-family:verdana;font-size:x-small;border:solid 1px black;filter:progid:DXImageTransform.Microsoft.dropshadow(OffX=2, OffY=2, Color='gray', Positive='true');}
  .StdText        {font-family:verdana;font-size:x-small;}
‹/style›


‹BODY bgcolor="ivory" style="font-family:verdana;font-size:small"›
‹h2›Orders and Customers‹/h2›

‹!-- ASP.NET Form --›
‹form runat="server"›

‹!-- Grid and the remainder of the page --›
‹table›‹tr›
‹td valign="top"›

‹asp:DataGrid id="grid" runat="server"
    AutoGenerateColumns="false"
    AllowPaging="true" PageSize="15"
    Font-Size = "xx-small"
    CellSpacing="0" CellPadding="4"
    DataKeyField="MyCustomerId"
    BorderStyle="solid" BorderColor="skyblue" BorderWidth="1" GridLines="both"
    OnItemCreated="ItemCreated"
    OnItemDataBound="ItemDataBound"
    OnPageIndexChanged="PageIndexChanged"›
    
    ‹headerstyle backcolor="skyblue" font-size="9pt" font-bold="true" /›
    ‹itemstyle backcolor="#eeeeee" /›
    ‹pagerstyle backcolor="skyblue" font-name="webdings" font-size="10pt" PrevPageText="3" NextPageText="4" /›
    
    ‹Columns›
          ‹asp:BoundColumn DataField="MyCustomerId" HeaderText="Customer" /›
          ‹asp:BoundColumn DataField="MyOrderId" HeaderText="Order #" /›
       ‹asp:BoundColumn DataField="price" HeaderText="Amount" DataFormatString="{0:c}"›
        ‹itemstyle horizontalalign="right" /›
       ‹/asp:BoundColumn›
    ‹/Columns›
‹/asp:DataGrid›

‹/td›
‹td valign="top" width="20px"›‹/td›
‹td valign="top"›
    ‹b›Year‹/b›
    ‹asp:dropdownlist runat="server" id="ddYears"›
        ‹asp:listitem runat="server" ›1998‹/asp:listitem›
        ‹asp:listitem runat="server" ›1997‹/asp:listitem›
        ‹asp:listitem runat="server" ›1996‹/asp:listitem›
    ‹/asp:dropdownlist>
    
    ‹asp:linkbutton runat="server" text="Load..." onclick="OnLoadYear" />
    ‹br>‹br>
    ‹asp:label runat="server" cssclass="StdText" id="lblMsg" />
‹/td>
‹/tr>‹/table>
‹hr>
‹/form>
‹/body>
‹/html›




珍惜愛你的人

-------------------------------------------------------------------------------
主 題:【珍惜愛你的人】
作 者:...
時空描述:發生在你我身邊的心情故事
--------------------------------------------------------------------------------
當你來到這個世界,她以手臂輕輕抱著保護你.你則以哭個像妖怪的聲音來謝謝她.

當你一歲時,她餵你也替你洗澡,你則以長夜大哭來謝謝她

當你二歲時,她教你走路,你會謝謝她,當她叫時卻溜得特別快

當你三歲時,她滿懷愛心的做飯給你吃,你則以滿地食物來謝謝她

當你四歲時,她教你繪畫,你則以滿間的彩色來謝謝她

當你五歲時,她在假日將你打扮的漂漂亮亮的,你則以噗通掉到一塘泥淖裡謝謝她

當你六歲時,她帶你去學校,你則以尖叫"我不去"來謝謝她

當你七歲時,她給你個棒球,你則以打破鄰居的窗戶來謝謝她

當你八歲時,她給你個冰淇淋,你以滿嘴的奶昔來謝謝她

當你九歲時,她讓你學鋼琴,你則以不曾練習來謝謝她

當你十歲時,她整天載你去上體育課踢足球及參加一個接著一個的生日Party,你則以頭也不回的跳出車外來謝她

當你十一歲時,她帶你和你的朋友去看電影時,你則以要求她坐在不同排來謝謝她

當你十二歲時,她警告你不要看某些TV Shows時,你則以等到她離開時注視這些TV Shows來謝謝她

在你十三歲的青少年時期,她建議你要去剪個頭髮時,你則以告訴她她一點品味都沒有,來謝謝她

當你十四歲時,她讓你去夏令營,你則以忘了寫封家書來謝謝她

當你十五歲時,她工作回來並期待一個擁抱,你則以房門深鎖來謝謝她

當你十六歲時,她教你如何開車,你以儘你所能的到處冒險來謝她

當你十七歲時,她正在等一個重要的電話時,你則以整夜電話中來謝謝她

當你十八歲時,她讓你去受高中教育,你則以外宿,到天明來謝謝她

後來...愈來愈大,
你已經十九歲,她讓你去念個大學,載你去學校,帶著你的袋子,你則以在宿舍門外,怕她會讓你在你朋友面前蒙羞,就趕緊說再見的方式來答謝她

當你已二十歲,她問你是否有約會,你則以 "那不關妳的事" 來答謝她

當你二十一歲,她建議你讓為你的未來找個好工作時,你則以"我才不想像妳一樣"的口氣來答謝她

當你二十二歲,她在你的畢業典禮緊緊的擁抱你,你則問她是否要付錢讓你去歐洲遊學來謝謝她

當你二十三歲,她替你的新公寓買個傢俱,你則告訴你的朋友,它實在是醜的不像話,的方式來謝謝她

當你二十四歲,她問你有關你的經濟及你未來的計劃,你則是拖長聲音的回謝她, "媽......媽,妳也拜託一下好不好"..很煩ㄟ..

當你二十五歲,她資助你的婚禮及高興的哭著對你說,她有多愛你,你則以搬離半個國家的距離隔絕她的方式來謝謝她

當你已三十歲,她跟你說她想要有個孫子可以抱,你則是非常謝謝她的跟她說," 時代不同,世事皆非了"

當你已四十歲,她提醒你要記得一個親人的生日,你則是謝謝她的告訴你,你"現在真的真的很忙"...忙到天昏地暗...

當你五十歲了,她身體不適而且需要你多多關心她,你則是以自己已是深責大任的父母來回謝她

然而,有一天她死了,你則發現你未替她做到任何事

讓我們花些時間為我們所稱呼的【媽媽】,
關心,付出,僅管有些人可能無法對他們的母親說出他們的愛.但她是無法取代的,

因為它是獨一無二的感情....

也許,她不是你最好的朋友,
也或許有些想法真的與你不同,
但她仍然是你的母親!
最敬愛也最疼您的母親.....

她總是在這裡聽你訴說你的喜怒哀樂,但請試著問問你自己吧!

你可有花足夠時間陪陪她? 可曾給過一絲一毫的時間聽她說看看她在廚房的困擾及疲勞?

你真的有嗎.....? 請您靜下心來回想從您懂事到現在的一切種種...母親對您的好...以貼心,愛心,尊重的心來對待她時,你會發現你會看到不同的觀點.

一旦錯失了,將只有美好回憶陪伴著你,而過去的遺憾也是不要將最貼心的人視為理所當然,請一輩子緊緊守住她們,假若沒有她們,生命將頓時失去意義。您覺得呢....
--------------------------------------------------------------------------------
後記:

記得韓嬰著作的韓詩外傳中有云
【樹欲靜而風不止,子欲養而親不待也】

詩經中亦云:
蓼蓼者莪,匪莪伊蒿。哀哀父母,生我斪勞!
父兮生我,母兮鞠我,拊我畜我,長我育我,
顧我復我,出入復我。欲報之德,昊天罔極!

我的朋友們,趁還來得及,
請『珍惜』與父母親相處的時光,
因為時空從不曾為任何人停留....




崑山湖的鴨子游出來了...

氣象局持續發布豪雨特報,嘉義以南及東部地區有局部性豪雨發生,其他地區也有局部性大雨,尤其是嘉義及台南地區有局部性大豪雨發生,屏東及高雄地區並有局部性超大豪雨,請注意防範落石、坍方、土石流,低窪地區應慎防淹水,雷雨區內也請慎防雷擊及強陣風。

attachments/932261.jpg

崑山湖的鴨子游出來了

attachments/033171.jpg

模範員工

attachments/139871.jpg

洪水氾濫

attachments/602958.jpg

水鄉澤國,不用到威尼斯也能享受

attachments/630154.jpg

超級大塞車,車子顧路,機車行老闆樂了


Vincent Lin (林同學)提供的崑山淹水實錄

http://photo.pchome.com.tw/cool_2002/0624*1/




網路銀行詐騙手法

今天收到一封SPAM,寫著由SOUTHTRUST BANK發出的會員通知信,上面寫著由於網路銀行軟體的更新,要客戶於線上確認其帳戶資料,我想這又是詐騙的手法了,按進去後才發現,之前銀行為了防止類似的詐騙案件,因此積極對客戶宣導進行網路銀行交易前必須先看清楚網址,但這個網站打開後,發現它的網址的確是SOUTHTRUST BANK的官方網址,但仔細看仍然可以發現它是用圖層變造的方式來掩蓋詐騙網站的真實位址,再者發現這封詐騙郵件是使用簡體的GB編碼,大概可以猜的出來應該是從大陸發出來的...

attachments/152058.gif


attachments/777454.jpg



官網針對詐騙郵件所發出的警示
http://www.southtrust.com/st/AboutUs/PrivacySecurity/Security/alerts/Alert_Update_12132004.htm




貝里斯禮炮儀隊

貝里斯禮炮儀隊 (不准笑) 有點給他好笑。
迎扁陣仗 禮輕情意重!陳水扁總統「共創雙贏、胸懷世界」沽名釣譽之旅第二站前往友邦貝里斯訪問,由於貝里斯並無 禮砲儀隊,因此是以簡單的小型禮砲配上類似散彈槍的子彈權充迎接陳總統的禮砲。
他們還有做道具,固定小禮砲喔! ^_^"註: 國防部要不要送幾門舊砲給他們擺擺場面,
真是第一次看到!貝里斯禮炮儀隊

attachments/521750.jpg