运行/累计总在GridView的列 [英] Running / Cumulative total in GridView column
问题描述
我一直在寻找一种方式来增加累计列到我的GridView控件,将显示一个累计行中的数字列之一。所以基本上:
I was looking for a way to add a cumulative total column to my GridView that would show a cumulative total of one of the numeric columns in the row. So basically:
Points | Running Total
2 | 2
1 | 3
-0.5 | 2.5
1.5 | 4
我看到<一的一些问题href=\"http://stackoverflow.com/questions/860966/calculate-a-running-total-in-sqlserver/10309947#10309947\">cumulative使用SQL Server 和其他数据库,但是我发现任何关于严格使用GridView控件不改变任何SQL合计,所以我想我会在这里发布我的解决方案。
I saw some questions on cumulative totals using SQL Server and other databases, but I found nothing about strictly using GridView without changing any SQL, so I thought I would post my solution here.
推荐答案
解决方案简单处理的RowDataBound,并增加了从给定的字段中的值到类的成员变量。那么该成员变量的值显示在运行总计列,其中包含用于这一目的的标签。
The solution simply handles RowDataBound and adds the value from the given field to a member variable of the class. Then the value in that member variable is displayed in the Running Total column, which holds a label for that purpose.
在GridView的ASPX code:
In the GridView aspx code:
<asp:GridView runat="server" ID="gvHistory" datasourceid="dsHistory" AutoGenerateColumns="false" AllowSorting="false"
onrowdatabound="gvHistory_RowDataBound">
<Columns>
<asp:BoundField HeaderText="Date" DataField="Date" SortExpression="Date" DataFormatString="{0:d}" />
<asp:BoundField HeaderText="Points" DataField="nPoints" />
<asp:TemplateField HeaderText="Running Total">
<ItemTemplate>
<asp:Label runat="server" ID="lblRunningTotal" Text=""></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
这里是code代表的RowDataBound处理程序
And here is the code for the RowDataBound handler
Protected m_runningTotal As Double = 0
Protected Sub gvHistory_RowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim pointsString As String = e.Row.DataItem("nPoints")
Dim points As Double
If Double.TryParse(pointsString, points) Then
m_runningTotal = m_runningTotal + points
Dim lblRunningTotal As Label = e.Row.FindControl("lblRunningTotal")
lblRunningTotal.Text = m_runningTotal.ToString
End If
End If
End Sub
这篇关于运行/累计总在GridView的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!