在VB.Net Gridview中添加总数 [英] Add total in VB.Net Gridview

查看:136
本文介绍了在VB.Net Gridview中添加总数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我尝试了以下方法:

 < asp:GridView ID =GVrunat =server
DataSourceID =SqlQueryEmptyDataText =No data>
<列>
< asp:BoundField DataField =WeekdayFooterText =HeaderText =Weekday/>
< asp:BoundField DataField =VolumeDataFormatString ={0:N0}
FooterText =。 HeaderText =音量/>
< /列>
< / asp:GridView>


受保护的子GV_rowdatabound(sender As Object,e As GridViewRowEventArgs)处理GV.RowDataBound
Dim Volume as integer = 0
对于每个r As GridViewRow在GV中。行
如果r.RowType = DataControlRowType.DataRow Then
Volume = Volume + CDec(r.Cells(1).Text)
End If
Next
GV。 FooterRow.Cells(1)。文本= Math.Round(音量,0)
结束子

这给了我一个错误信息:

对象引用未设置为对象实例



I接下来的页面中的建议,我改变了代码:
试图总gridview在asp中

  Sub GV_WeekSumary_rowcreated(ByVal sender As Object,ByVal e As GridViewRowEventArgs)
Dim Volume as integer = 0
对于每个r As GridViewRow在GV.Rows中
如果r.RowType = DataCont rolRowType.DataRow Then
Volume = Volume + CDec(r.Cells(1).Text)
End If
Next

如果e.Row.RowType = DataControlRowType .Footer然后
e.Row.Cells(1)。文本= Math.Round(音量,0)
结束如果
结束子

这不会产生错误,但页脚不会显示任何值。



已经尝试过以下内容:

  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()
结束子

在页脚仍然没有值,但是当我debbug它我可以看到,页脚被assisgned价值我需要。为什么它不显示在网站上?



任何想法如何让这个工作?

。解决方案

您必须使用数据绑定事件



试试这个:

  Protected Sub GV_DataBound(ByVal sender As Object,ByVal e As System.EventArgs)处理GV.DataBound 
Dim Volume As Decimal = 0
For Each r作为GridViewRow在GV.Rows
如果r.RowType = DataControlRowType.DataRow然后
Volume + = Convert.ToDecimal(r.Cells(1).Text)
End If
Next
GV.FooterRow.Cells(1).Text = Math.Round(Volume,0).ToString()
End Sub


I can not manage to get the gridview display the total in a footer

I've tried the following:

<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

This gives me an error messages:

Object reference not set to an instance of an object

I followed advice in the following page and I changed the code: 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

This does not give an error, but the footer does not show any value.

I've tried also the following:

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

Still no value in the footer, but when I debbug it I can see that footer is assisgned the value I need. Why it is not displayed in the website?

Any idea how I can get this to work?

解决方案

You must use DataBound event.

Try this:

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

这篇关于在VB.Net Gridview中添加总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆