最常用的合計方式
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound" ShowFooter="True">
<Columns>
<asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
<asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" SortExpression="UnitPrice" />
<asp:BoundField DataField="Quantity" HeaderText="Quantity" SortExpression="Quantity" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NORTHWNDConnectionString %>" SelectCommand="SELECT B.ProductName,A.UnitPrice,A.Quantity
FROM [dbo].[Order Details] A
LEFT JOIN [dbo].[Products] B
ON A.ProductID = B.ProductID"></asp:SqlDataSource>
static int UnitPriceTotal = 0;
static int total = 0;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
UnitPriceTotal += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "UnitPrice"));
total += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Quantity"));
}
if(e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[0].Text = "Grand Total";
e.Row.Cells[0].Font.Bold = true;
e.Row.Cells[1].Text = UnitPriceTotal.ToString();
e.Row.Cells[1].Font.Bold = true;
e.Row.Cells[2].Text = total.ToString();
e.Row.Cells[2].Font.Bold = true;
}
}