2011-01-29 71 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

採取網格視圖元素的數據表,計算總每一列的編程方式, 使網格視圖頁腳模板,插入標籤,發現綁定您想要的事件和標籤數據。