求和列值并在C#中显示在页脚中

时间:2009-06-06 08:43:16

标签: asp.net gridview aggregate

我有以下代码,用于通过SQLDATASOURCE显示ItemID,Name和Price。如何在Price列中获取值的SUM并将其显示在页脚中?

<asp:GridView ID="GridView2" runat="server" ShowFooter="true" AutoGenerateColumns="False" DataKeyNames="ItemID"
    DataSourceID="SqlDataSource2" Style="position: relative"  >
    <Columns>
        <asp:BoundField DataField="ItemID" HeaderText="ItemID" InsertVisible="False" ReadOnly="True"
            SortExpression="ItemID" />
        <asp:BoundField DataField="Name" HeaderText="Name" FooterText="Total" FooterStyle-Font-Bold="true" SortExpression="Name" />
        <asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />
        <ItemTemplate>
    </Columns>
</asp:GridView>

您的帮助非常明确。

谢谢,

1 个答案:

答案 0 :(得分:2)

((Label)GridView1.FooterRow.Cells[1].FindControl("Label2")).Text = 
myDataSet.Tables[0].Compute("sum(unitsinstock)", "").ToString();

在此处获取更多详细信息:http://programming.top54u.com/post/ASPNet-20-GridView-Compute-Column-Sum-using-C-sharp.aspx