我们在实现报表时通常使用水晶报表或者其他的报表工具,其实在SQL Server和DataGrid的协调下,也能实现一些报表的功能,这片文章描述了如何实现在DataGrid上实现对数据的合计和摘要,其数据的组织主要是利用SQL Server实现的,在显示上利用DataGrid控制.
在实现数据的合计和摘要使用了SQL Server 的ROLLUP和CUBE,(可以参见SQL Server中汇总功能的使用GROUPING,ROLLUP和CUBE)
这是对DataGrid绑定时的操作:
Private
Sub GrdKY_ItemDataBound()
Sub GrdKY_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles GrdKY.ItemDataBound
Dim drv As DataRowView
Dim dr As DataRow
Dim sCustomerID As String
drv = e.Item.DataItem
If IsNothing(drv) Then Exit Sub
If IsDBNull(drv) Then Exit Sub
If CInt(drv("OrderID")) = -1 Then
sCustomerID = drv("CustomerID").ToString
dr = GetCustomerInfoRow(iYear, sCustomerID)
If IsNothing(dr) Then Exit Sub
Dim sb As New StringBuilder("<b>")
sb.Append("</b> 订单数量: <b>")
sb.Append(dr("totalorders").ToString())
sb.Append("</b> 公司名称: <b>")
sb.Append(dr("companyname").ToString())
sb.Append("</b>")
e.Item.Cells(0).Text = sb.ToString()
Dim s As String
s = String.Format("{0:c}", dr("total"))
e.Item.Cells(1).Text = s
e.Item.BackColor = Color.White
e.Item.Cells(2).Font.Bold = True
e.Item.Cells(2).HorizontalAlign = HorizontalAlign.Right
e.Item.Cells.RemoveAt(1)
e.Item.Cells(0).ColumnSpan = 2
End If
End Sub
Dim drv As DataRowView
Dim dr As DataRow
Dim sCustomerID As String
drv = e.Item.DataItem
If IsNothing(drv) Then Exit Sub
If IsDBNull(drv) Then Exit Sub
If CInt(drv("OrderID")) = -1 Then
sCustomerID = drv("CustomerID").ToString
dr = GetCustomerInfoRow(iYear, sCustomerID)
If IsNothing(dr) Then Exit Sub
Dim sb As New StringBuilder("<b>")
sb.Append("</b> 订单数量: <b>")
sb.Append(dr("totalorders").ToString())
sb.Append("</b> 公司名称: <b>")
sb.Append(dr("companyname").ToString())
sb.Append("</b>")
e.Item.Cells(0).Text = sb.ToString()
Dim s As String
s = String.Format("{0:c}", dr("total"))
e.Item.Cells(1).Text = s
e.Item.BackColor = Color.White
e.Item.Cells(2).Font.Bold = True
e.Item.Cells(2).HorizontalAlign = HorizontalAlign.Right
e.Item.Cells.RemoveAt(1)
e.Item.Cells(0).ColumnSpan = 2
End If
End Sub