如何使用 Visual Basic .NET 在 ASP.NET 中创建 DataGrid 的摘要行

本文分步介绍如何使用 Visual Basic .NET 为 ASP.NET 中的 DataGrid 控件创建摘要行。

原始产品版本: Visual Basic .NET
原始 KB 编号: 313154

摘要

在本文中,你将创建一个 Visual Basic .NET 项目,添加代码以从 Northwind 数据库访问名为“按金额分配的销售额总计”视图,然后将“按金额计算的销售额”视图绑定到 DataGrid。 将数据绑定到 DataGrid 时, ItemDataBound 此示例使用 DataGrid 的 事件来汇总 SaleAmount 字段。 此示例还使用 DataGrid 的页脚来显示摘要或总计。

本文引用 Microsoft .NET Framework 类库命名空间 System.Data.SqlClient

注意

有关本文的 Microsoft Visual C# .NET 版本,请参阅 326339

要求

以下列表概述了所需的建议硬件、软件、网络基础结构和服务包:

  • Microsoft Windows
  • Microsoft .NET Framework
  • Microsoft Visual Studio .NET
  • Microsoft Internet Information Services (IIS)
  • Microsoft SQL Server 7.0 或更高版本和 Northwind 数据库

注意

Northwind 数据库包含在 SQL sServer 7.0 及更高版本中。

创建 Visual Basic .NET 项目并添加 DataGrid

在本部分中,将创建一个 Visual Basic .NET 项目,选择 DataGrid 的格式,然后将 DataGrid 设置为显示页脚。 由于使用页脚显示摘要,因此请务必显示页脚。

注意

默认情况下, ShowFooter 属性处于关闭状态。

  1. 启动 Visual Studio .NET。 此时会显示 Visual Studio .NET IDE。

  2. 在"文件"菜单上,指向"新建",然后单击"项目"。

  3. 在“新建项目”对话框中,单击“项目类型”下的“Visual Basic 项目”,然后单击“模板”下的“ASP.NET Web 应用程序”。

  4. 在“ 新建项目 ”对话框中,请注意 ,“名称 ”框不可用, (它显示为灰色) 。 “ 位置 ”框包含以下文本 (或类似) :
    http://localhost/WebApplication1

    将位置更改为 http://localhost/SummaryRow,然后单击“ 确定”。 将创建一个新项目,其中包括名为 WebForm1.aspx 的 Web 窗体。

  5. 在解决方案资源管理器中,双击“WebForm1.aspx”。

  6. 将 DataGrid 控件从工具箱拖动到窗体。

  7. 右键单击 DataGrid 控件,然后单击“ 自动设置格式”。 单击“ 彩色 1”,然后单击“ 确定”。

  8. 右键单击 DataGrid 控件,然后单击“ 属性”。 在“ 属性 ”对话框中,将 ShowFooter 属性的值更改为 True

编写代码以访问数据库

在本部分中,使用 Northwind 数据库中的“按金额统计的销售额总计”视图来计算 SalesAmount 字段的摘要。 “按金额计算的销售总计”视图包括“订单”、“公司名称”和“SalesAmount”字段。

  1. 在 IDE 中,右键单击 Web 窗体,然后单击“ 查看代码”。

  2. 在代码隐藏窗口中,将以下代码添加到页面顶部:

    Imports System.Data
    Imports System.Data.SqlClient
    
  3. 在类声明部分添加以下代码:

    Private myTotal As System.Double 'This variable tracks the running total.
    
  4. 将 事件中的 Page_Load 代码替换为以下代码:

    Private Sub Page_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
        'Connect to the database, retrieve data, and then fill the data in the DataSet.
        Dim myConnection As New SqlConnection("server=(local)\netsdk;"Integrated Security=SSPI" & _
        pwd=;database=northwind")
        Dim myDataAdapter As New SqlDataAdapter("SELECT top 15 [OrderID], [CompanyName], " & _
        "[SaleAmount] FROM [Northwind].[dbo].[Sales Totals by Amount]", myConnection)
        Dim myDataSet As New DataSet()
        myDataAdapter.Fill(myDataSet)
        'Set the DataSource for the DataGrid, and then bind the data.
        DataGrid1.DataSource = myDataSet
        DataGrid1.DataBind()
    End Sub
    
  5. 根据环境修改连接字符串。

使用 ItemDataBound 事件

ItemDataBound 项是绑定到 DataGrid 控件的数据之后引发事件。 此事件提供在客户端上显示数据项之前访问数据项的最后机会。 引发此事件后,数据项为 null,不再可用。

对于数据绑定的每个项,必须检查 ItemType 属性。 如果 ItemType 类型 Item 为 或 AlternatingItem,则从项的最后一个单元格(包含 SaleAmount 值)接收值。 在此示例中,将此值添加到正在运行的摘要变量。 ItemType如果 为 Footer,则会收到来自所有行的总计。 因此,将摘要变量的值分配给最后一个单元格的文本值。

注意

此代码使用格式设置表达式来为 SaleAmount 数据提供统一的外观。

在 事件后面 Page_Load 添加以下代码:

Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
    Select Case e.Item.ItemType
    Case ListItemType.AlternatingItem, ListItemType.Item
    'Calculate total for the field of each row and alternating row.
    myTotal += CDbl(e.Item.Cells(2).Text)
    'Format the data, and then align the text of each cell to the right.
    e.Item.Cells(2).Text = Format(CDbl(e.Item.Cells(2).Text), "##,##0.00")
    e.Item.Cells(2).Attributes.Add("align", "right")
    Case ListItemType.Footer
    'Use the footer to display the summary row.
    e.Item.Cells(1).Text = "Total Sales"
    e.Item.Cells(1).Attributes.Add("align", "left")
    e.Item.Cells(2).Attributes.Add("align", "right")
    e.Item.Cells(2).Text = myTotal.ToString("c")
    End Select
End Sub

生成项目并测试代码

  1. 在“文件”菜单上单击“全部保存”
  2. 在"构建"菜单上,单击"构建解决方案"。
  3. 在解决方案资源管理器中,右键单击“.aspx”页,然后单击“在浏览器中查看”。 .aspx页显示在浏览器中,DataGrid 显示 OrderID、CompanyName 和 SaleAmount 列。 请注意,页脚显示 SalesAmount 列的总计。

References