无法在Gridview中显示列的总和

时间:2012-10-24 17:44:13

标签: c# asp.net

我无法在页脚中显示标签中的值。以下是代码

Aspx

<asp:GridView ID="gvallAccount" runat="server" AutoGenerateColumns="False" Width="589px"
                        OnRowDataBound="gvallAccount_RowDataBound" OnRowCommand="gvallAccount_RowCommand">
                        <Columns>
                            <asp:TemplateField Visible="False">
                                <ItemTemplate>
                                    <asp:Label ID="lblAccountID" runat="server" Text='<%# Bind("ProductAccountID") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Name" Visible="True">
                                <ItemTemplate>
                                    <asp:Label ID="lblProductName" runat="server" Text='<%# Bind("ProductName") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Account Number" Visible="True">
                                <ItemTemplate>
                                    <asp:LinkButton ID="lnkAccCode" runat="server" Text='<%# Bind("ProductAccountCode") %>'
                                        OnClick="lnkAccCode_Click" CommandName="gotoLink"></asp:LinkButton>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Account Value(KES)" Visible="True">
                                <ItemTemplate>
                                    <asp:Label ID="lblBalance" runat="server" Text='<%# Bind("BalanceToDate") %>'></asp:Label>
                                </ItemTemplate>
                                <FooterTemplate>
                                    <asp:Label ID="lblTotal" runat="server"></asp:Label>
                                </FooterTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>

C# Code 

 protected void gvallAccount_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                decimal rowTotal = Convert.ToDecimal
                            (DataBinder.Eval(e.Row.DataItem, "BalanceToDate"));
                grdTotal = grdTotal + rowTotal;
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                Label lblTotal = (Label)e.Row.FindControl("lblTotal");
                lblTotal.Text = grdTotal.ToString();
            }
        }
        catch (Exception Ex)
        {
            logger.Error("gvallAccount_RowDataBound : " + Ex.Message);
        }
    }

1 个答案:

答案 0 :(得分:0)

您需要在GridView标记中使用ShowFooter =“True”属性。

<asp:GridView ID="gvallAccount" runat="server" AutoGenerateColumns="False" Width="589px"
                    OnRowDataBound="gvallAccount_RowDataBound" OnRowCommand="gvallAccount_RowCommand" ShowFooter="True">