日期:2014-05-19  浏览次数:20556 次

如何在GridView的Footer内显示总计?
如题,要详细代码,谢谢

------解决方案--------------------
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

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

{

DataRowView drvBasicCountInfor = (DataRowView)e.Row.DataItem;

iNumber = Convert.ToInt32(drvBasicCountInfor[7]);

iMoney = Convert.ToInt32(drvBasicCountInfor[8]);

}



// 生成总计

//totalNumber += iMemberNo;

//totalMoney += iVisitNo;

totalNumber += iNumber;

totalMoney += iMoney;





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

{

e.Row.Cells[6].Text = "总计 ";

e.Row.Cells[7].Text = Convert.ToString(totalNumber-iNumber);

e.Row.Cells[8].Text = Convert.ToString(totalMoney-iMoney);

}



}


------解决方案--------------------
前台:
<asp:GridView ID= "GridView2 " runat= "server "
AutoGenerateColumns= "False "
OnRowDataBound= "GridView1_RowDataBound "
ShowFooter= "true ">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID= "labelfirst " runat= "server " Text= ' <%# Eval( "first ") %> '> </asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label id= "labelAll " runat= "server " Text= "总计: "> </asp:Label>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
后台:
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 "是我数据源的一个字段