top
Loading...
用ROLLUP進行分類數據統計(二)

編者按:上一篇文章《用ROLLUP進行分類數據統計(一)》我們介紹了ms sql server中的roll up語句。下面開始介紹如何用datagrid結合rollup語句來進行分類統計。

我們要達到的效果是這樣的:

首先,應先將數據庫中的產品數據按照所屬的不同的目錄列舉出來,這其中要用到一些技巧,詳細的可以參考《在DATAGRID中使用分類標題》一文(http:// http://tech.ccidnet.com/pub/article/c1110_a70590_p1.html)。這里先用SQL語句,從數據庫讀取product表的數據,之后放到dataset的默認datatable中去,然后檢查每一個產品所屬的類別,如果發現某一個產品的類別和前一條記錄中產品所屬的類別不一樣的話,那么就可以肯定當前產品是屬于一個新的分類了,就可以插入新的行,并且加以修飾,成為分類標題,同時將roll up的統計結果顯示在相應的位置就可以了。我們先來看page_load部分的代碼

Sub Page_Load(Sender As Object, E As EventArgs) Handles MyBase.Load    ' TODO: Update the ConnectionString and CommandText values for your applicationdim  ConnectionString as string = "server=localhost;database=northwind;UID=sa"    Dim CommandText As String = "Select CASE WHEN (Grouping(CategoryName)=1) THEN " & _              "'MainTotal' ELSE categoryname END AS CategoryName, "    CommandText &= " CASE WHEN (Grouping(ProductName)=1) THEN 'SubTotal' ELSE " & _              "Productname END AS ProductName,"    CommandText &= " Sum(UnitPrice) as unitprice, "    CommandText &= " Sum(UnitsinStock) as UnitsinStock "    CommandText &= " from Products INNER JOIN Categories On Products.categoryID = " & _                    " Categories.CategoryID"    CommandText &= " Group By Categoryname, ProductName WITh ROLLUP "    Dim myConnection As New SqlConnection(ConnectionString)    Dim myCommand As New SqlDataAdapter(CommandText, myConnection)    Dim ds As New DataSet    myCommand.Fill(ds)    Dim curCat As String  ‘指示當前記錄中產品所屬的類別    Dim prevCat As String  ‘指示上一條記錄中產品所屬的類別    Dim i As Integer = 0   ‘要插入分類標題行的位置,用I表示'遍歷結果集,找出要插入分類標題的行    Do While i <= ds.Tables(0).Rows.Count - 1        curCat = ds.Tables(0).Rows(i).Item(0)        If curCat <> prevCat Then  ‘如果發現前后兩記錄的所屬類別不一樣            prevCat = curCat            Dim shRow As DataRow = ds.Tables(0).NewRow            shRow(1) = ds.Tables(0).Rows(i).Item(0)                        'Change ItemDataBound marker to Negative Number            shRow(2) = -1  ‘‘設置一個臨時的標記                ds.Tables(0).Rows.InsertAt(shRow, i)                      i += 1        End If             i += 1     Loop    ‘將最后一行的標題改為total    ds.Tables(0).Rows(ds.Tables(0).Rows.Count - 1).Item(1) = "Total"    DataGrid1.DataSource = ds    DataGrid1.DataBind()End Sub

可以看到,上面用到的技巧基本和《在DATAGRID中使用分類標題》的是差不多的,只不過為了在最后統計所有的分類結果,將最后一行的標題改為total。之后,我們要對datagrid中的數據進行格式化,將臨時標記取消,換為我們要顯示的格式,因為新插入的分類標題行是和普通的行不同的,我們要設置其樣式,于是在其item_bound事件中,代碼如下:

Private Sub DataGrid1_ItemDataBound(sender As Object,  e As DataGridItemEventArgs)Select Case e.Item.ItemType        Case ListItemType.AlternatingItem, ListItemType.Item  ’如果發現是分類標題行的話,則對其進行格式化            If e.Item.Cells(1).Text.Equals("-1") Then                'Format the SubHeading Columns                e.Item.Cells(0).Attributes.Add("align", "Left")                e.Item.Cells(0).ColumnSpan = 3                e.Item.Cells(0).Font.Bold = True     ‘合拼為一個新的分類標題行,移除其中的單元格                e.Item.Cells.RemoveAt(2)                 e.Item.Cells.RemoveAt(1)                e.Item.BackColor = Color.FromArgb(204,204,255)End If‘最后的所有分類的總計            If e.Item.Cells(0).Text.Equals("Total") Then                'Format the Main total column                e.Item.Cells(0).Attributes.Add("align", "Left")                e.Item.Cells(0).Font.Bold = True                e.Item.Cells(1).Font.Bold = True                e.Item.Cells(2).Font.Bold = True                e.Item.BackColor = Color.FromArgb(204,153,255)End If '如果是每個分類的小計的話,則設置相應的顯示格式            If e.Item.Cells(0).Text.Equals("SubTotal") Then                'Format the subtotal columns.                e.Item.Cells(0).Attributes.Add("align", "Left")                e.Item.Cells(0).Text = "Sub Totals"                e.Item.Cells(0).Font.Bold = True                e.Item.Cells(1).Font.Bold = True                e.Item.Cells(2).Font.Bold = True                e.Item.Cells(0).Font.Italic = True                e.Item.Cells(1).Font.Italic = True                e.Item.Cells(2).Font.Italic = True             End If    End SelectEnd Sub

本文例子運行的效果可以在http://aspnet.4guysfromrolla.com/demos/dgRollup.aspx中看到

最后,別忘記了Imports System.Data.SqlClient咯。運行程序后,就會得到本文圖所示的效果了。

本程序在win2000 server+vs.net 2002下通過,也可以在vs.net 2003下運行。

小編說話:如果你想闡述自己的觀點,請在下面的“發表評論”中發言

(責任編輯:趙紀雷)

作者:http://www.zhujiangroad.com
來源:http://www.zhujiangroad.com
北斗有巢氏 有巢氏北斗