从GridView获取特定列的总值 [英] Getting total values of a certain column from GridView
问题描述
他
我正在将ASP.NET/VB.NET与SQL-Server-2012一起使用.
I am using a ASP.NET/VB.NET with SQL-Server-2012.
我有一个GridView列,其中包含3个字段和1个模板字段,如下所示:
I have a a GridView column with 3 fields and 1 template field as shown below:
<asp:GridView ID="grdItems" runat="server" AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlDataSource3" Font-Names="Tahoma" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="item_name" HeaderText="Item" SortExpression="item_name" />
<asp:BoundField DataField="item_cost" HeaderText="Cost (inc. VAT)" SortExpression="item_cost" />
<asp:BoundField DataField="item_quantity" HeaderText="Quantity" SortExpression="item_quantity" />
<asp:TemplateField HeaderText="Sub-Total (inc. VAT)">
<ItemTemplate>
<asp:Label ID="TextBox3" runat="server" Text='<%# Convert.ToInt32(Eval("item_quantity")) * Convert.ToDouble(Eval("item_cost"))%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<FooterTemplate>
<asp:Label ID="lblTotalPrice" runat="server" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" VerticalAlign="Middle" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" HorizontalAlign="Center" VerticalAlign="Middle" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
后面的代码:
Imports System.Data.SqlClient
Imports System.Data
Partial Class ProjectReport
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim ProjectID = Session("project_id")
Session("ProjectID") = ProjectID
End Sub
Protected Sub grdItems_RowDataBound(sender As Object, e As GridViewRowEventArgs)
Dim totalPrice As Decimal = 0
If e.Row.RowType = DataControlRowType.DataRow Then
Dim lblPrice As Label = DirectCast(e.Row.FindControl("lblTotalPrice"), Label)
Dim price As Decimal = [Decimal].Parse(lblPrice.Text)
totalPrice += price
End If
If e.Row.RowType = DataControlRowType.Footer Then
Dim lblTotalPrice As Label = DirectCast(e.Row.FindControl("lblTotalPrice"), Label)
lblTotalPrice.Text = totalPrice.ToString()
End If
End Sub
End Class
DataBound()
Private Sub BindData()
Dim conn As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True")
Dim query As New SqlCommand("SELECT Items.item_name, Items.item_cost, project_items.item_quantity FROM Items INNER JOIN project_items ON items.item_id = project_items.item_id WHERE project_items.project_id = @parameter", conn)
query.Parameters.AddWithValue("@UserID", Session("ProjectID"))
Dim da As New SqlDataAdapter(query, conn)
da.SelectCommand = query
Dim table As New DataTable()
da.Fill(table)
grdItems.DataSource = table
grdItems.DataBind()
End Sub
最后一列(模板字段)将数量字段与成本字段相乘.
The last column (the template field) multiplies the quantity field with the cost field.
如何计算(通过添加)模板字段中的所有值?
How do I calculate all the values (by adding) in the template field?
推荐答案
您必须使用数据绑定事件对值求和.请参见此示例,以适应您的需求:>
You must use the databinding events to sum the values. See this example and adapt to your needs:
private Decimal OrderTotal;
protected void GridView1_DataBinding(object sender, EventArgs e)
{
OrderTotal = 0.0M;
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Keep adding the subtotal here
OrderTotal += Subtotal;
}
}
protected void GridView1_DataBound(object sender, EventArgs e)
{
//Set a control with the total sum
LabelOrderTotal.Text = OrderTotal.ToString("C");
}
基本上,您一直将RowDataBound
事件中的值相加,并在DataBound
事件中使用总和设置标签.另外,您可以在DataBound
事件中遍历网格并添加所有内容.
Basically you keep adding the values in the RowDataBound
event and in the DataBound
event you set a label with the total sum. Alternatively, you can iterate over your grid in the DataBound
event and add everything up.
这篇关于从GridView获取特定列的总值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!