2011-01-29 197 views
1

我有一个网格视图,其中包含小时作为其中一列..现在,我需要在页脚总小时数(所有行的小时数总和)..我们如何添加?请帮助gridview页脚

回答

0

随着每一行被渲染(即当它被称为RowDataBound事件),你需要保持运行每行的总时间,下面的代码应该这样做。 (来自MSDN站点的示例的修改版本)

一旦找到页脚的RowDataBound事件,就会显示总小时数。

' This will keep the running total for the number of hours, and should be placed at the top of your page class 

Dim Hours as Integer 


Sub detailsGridView_RowDataBound(ByVal sender As Object, _ 
    ByVal e As GridViewRowEventArgs) 

    If e.Row.RowType = DataControlRowType.DataRow Then 

     Hours += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "Hours")) 

    ElseIf e.Row.RowType = DataControlRowType.Footer Then 
     e.Row.Cells(0).Text = "Totals Hours:" 
     e.Row.Cells(1).Text = Hours.ToString("c")   
     e.Row.Cells(1).HorizontalAlign = HorizontalAlign.Right 
     e.Row.Cells(2).HorizontalAlign = HorizontalAlign.Right 
     e.Row.Font.Bold = True 
    End If 
End Sub 
1
Imports System.Data 
Imports System.Data.SqlClient 


Partial Class _Default 
Inherits System.Web.UI.Page 

Protected Sub Page_Load(ByVal sender As Object, _ 
    ByVal e As System.EventArgs) Handles Me.Load 

    If Not IsPostBack Then 
     ' 検索を行う 
     Dim cn As New SqlConnection(_ 
      SqlDataSource1.ConnectionString) 
     Dim da As New SqlDataAdapter(_ 
      "SELECT count(*), sum(age) FROM person", cn) 
     Dim dt As New DataTable 
     da.Fill(dt) 
     ' 人数と年齢の合計を取得する 
     Dim n As Integer = CType(dt.Rows(0)(0), Integer) ' 人数 
     Dim sum As Integer = CType(dt.Rows(0)(1), Integer) ' 年齢の合計 
     ' フッターに設定する 
     GridView1.Columns(0).FooterText = String.Format("{0}人", n) 
     GridView1.Columns(1).FooterText = String.Format("合計{0}", sum) 
    End If 
End Sub 
End Class 
0

采取网格视图元素的数据表,计算总每一列的编程方式, 使网格视图页脚模板,插入标签,发现绑定您想要的事件和标签数据。