如何在GridView的Footer内显示总计

2025-05-19 21:05:21
推荐回答(1个)
回答(1):

前台:

ID="GridView2" runat="server" AutoGenerateColumns="False"

OnRowDataBound="GridView1_RowDataBound"

ShowFooter="true">







ID="labelfirst" runat="server" Text='<%# Eval("first") %>'>





id="labelAll" runat="server" Text="总计:">









后台:

private int num = 0;

protected void GridView1_RowDataBound(object
sender, GridViewRowEventArgs e)

{

if
(e.Row.RowType == DataControlRowType.DataRow)

{

DataRowView dr = e.Row.DataItem as
DataRowView;

num += Convert.ToInt32(dr.Row["first"]);

}

else
if (e.Row.RowType == DataControlRowType.Footer)

{

Label LabelAll = e.Row.FindControl("labelAll") as
Label;

if
(LabelAll != null)

{

LabelAll.Text += num.ToString();//"计算的总数,或者也可以单独计算";//

}

}

}

"first"是数据源的一个字段