2013-09-16 26 views
2

我不能設法得到GridView控件顯示總在頁腳加入總在VB.Net的GridView

我已經試過如下:

<asp:GridView ID="GV" runat="server" 
    DataSourceID="SqlQuery" EmptyDataText="No data"> 
    <Columns> 
     <asp:BoundField DataField="Weekday" FooterText=" " HeaderText="Weekday" /> 
     <asp:BoundField DataField="Volume" DataFormatString="{0:N0}" 
      FooterText="." HeaderText="Volume" />    
    </Columns> 
</asp:GridView> 


Protected Sub GV_rowdatabound(sender As Object, e As GridViewRowEventArgs) Handles GV.RowDataBound 
    Dim Volume as integer = 0 
    For Each r As GridViewRow In GV.Rows 
     If r.RowType = DataControlRowType.DataRow Then 
      Volume = Volume + CDec(r.Cells(1).Text) 
     End If 
    Next 
    GV.FooterRow.Cells(1).Text = Math.Round(Volume , 0) 
End Sub 

這給了我一個錯誤信息:

對象引用不設置到對象的實例

我也跟着下頁意見,我改變了代碼: trying to total gridview in asp

Sub GV_WeekSumary_rowcreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs) 
    Dim Volume as integer = 0 
    For Each r As GridViewRow In GV.Rows 
     If r.RowType = DataControlRowType.DataRow Then 
      Volume = Volume + CDec(r.Cells(1).Text) 
     End If 
    Next 

    If e.Row.RowType = DataControlRowType.Footer Then 
     e.Row.Cells(1).Text = Math.Round(Volume , 0) 
    End If 
End Sub 

這不會給出錯誤,但頁腳不顯示任何值。

我試過還有如下:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
    Dim Volume as integer = 0 
    For Each r As GridViewRow In GV.Rows 
     If r.RowType = DataControlRowType.DataRow Then 
      Volume = Volume + CDec(r.Cells(1).Text) 
     End If 
    Next 
    GV.FooterRow.Cells(1).Text = Math.Round(Volume, 0) 
    GV.DataBind() 
End Sub 

還是在頁腳沒有價值,但是當我debbug它,我可以看到,頁腳assisgned我需要的價值。爲什麼它不顯示在網站上?

任何想法如何讓這個工作?

回答

2

您必須使用DataBound事件。

試試這個:

Protected Sub GV_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles GV.DataBound 
    Dim Volume As Decimal = 0 
    For Each r As GridViewRow In GV.Rows 
     If r.RowType = DataControlRowType.DataRow Then 
      Volume += Convert.ToDecimal(r.Cells(1).Text) 
     End If 
    Next 
    GV.FooterRow.Cells(1).Text = Math.Round(Volume, 0).ToString() 
End Sub 
+0

感謝您的答覆。這是一個錯誤,我糾正了。但是,我看不到網站上的數據 – Selrac

+0

是的,是的!謝謝。 Databound而不是rowbound,然後 – Selrac