我已经为你提供了示例代码...
在您的代码中引用此代码,它将动态计算总值。使用 DataRowBound 属性,它的工作...
ASP 代码:
<asp:GridView ID="grdBoardingListReport" runat="server" CellPadding="2" ForeColor="#333333" GridLines="Both" AutoGenerateColumns="false" ShowFooter="false">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#c5c5c5" Font-Bold="True" HorizontalAlign="Right" />
<HeaderStyle CssClass="clsReportGridHeader" BackColor="#c5c5c5" Font-Size="10"/>
<PagerStyle BackColor="#39afd9" ForeColor="White" HorizontalAlign="Center"/>
<Columns>
<asp:BoundField DataField="Sl.No" HeaderText="slno" />
<asp:BoundField DataField="fd_name" HeaderText="Name" />
<asp:BoundField DataField="fd_total" HeaderText="Total"/>
</Columns>
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
代码绑定:
private void bindFreePassReport()
{
if (!(Session["FreePassReceive"] == null))
{
DataTable freePassReceive = (DataTable)Session["FreePassReceive"];
if (freePassReceive.Rows.Count > 1)
{
grdFreePassReport.DataSource = freePassReceive;
grdFreePassReport.DataBind();
lblTitle.Text = Session["FreePassReportTitle"].ToString();
}
else
{
grdFreePassReport.DataSource = null;
grdFreePassReport.DataBind();
}
}
}
数据行绑定:
int Total;
protected void grdBordingPlace_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
total += Convert.ToInt32(e.Row.Cells[2].Text);
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[1].Text = "Total" + " : ";
e.Row.Cells[2].Text = total.ToString();
}
}