用ROLLUP进行分类数据统计(二)综合教程 -电脑资料

电脑资料 时间:2019-01-01 我要投稿
【www.unjs.com - 电脑资料】

   

    我们介绍了ms sql server中的roll up语句,

用ROLLUP进行分类数据统计(二)综合教程

。下面开始介绍如何用datagrid结合rollup语句来进行分类统计。

    我们要达到的效果是这样的:

   

   

    首先,应先将数据库中的产品数据按照所属的不同的目录列举出来,这其中要用到一些技巧.这里先用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 application

    dim 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 &lsquo

    关 键 字:SQLServer

    相关文章:

    SQL Server事务日志的几个常用操作

    SQL Server:存储图像和BLOB文件(一)

    SQL Server:存储图像和BLOB文件(二)

    SQL Server:存储图像和BLOB文件(三)

    SQL Server:存储图片和BLOB文件(四)

    ;指示上一条记录中产品所属的类别

    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,

电脑资料

用ROLLUP进行分类数据统计(二)综合教程》(https://www.unjs.com)。之后,我们要对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

    关 键 字:SQLServer

    相关文章:

    SQL Server事务日志的几个常用操作

    SQL Server:存储图像和BLOB文件(一)

    SQL Server:存储图像和BLOB文件(二)

    SQL Server:存储图像和BLOB文件(三)

    SQL Server:存储图片和BLOB文件(四)

    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 Select

    End Sub

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

    最后,别忘记了Imports System.Data.SqlClient咯。运行程序后,就会得到本文图所示的效果了。

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

    关 键 字:SQLServer

    相关文章:

    SQL Server事务日志的几个常用操作

    SQL Server:存储图像和BLOB文件(一)

    SQL Server:存储图像和BLOB文件(二)

    SQL Server:存储图像和BLOB文件(三)

    SQL Server:存储图片和BLOB文件(四)

   

最新文章