0

我正在尝试在将显示在页脚中的 GridView 表中创建所有值的总计,我从创建占位符开始,但不确定如何创建总计

        <FooterTemplate>
            <asp:Label ID="lblGrandTotal" runat="server" Text=""></asp:Label>
        </FooterTemplate>                  
    </asp:TemplateField>               
</Columns>
4

2 回答 2

1

如果您只添加一列,这应该可以工作..

代码隐藏 C#

decimal totalA = 0;

protected void gvAlexandria_RowDataBound(object sender, GridViewRowEventArgs e)
{
    string totalAmtFinanced = ((Label)gvVehicleTEMP.FooterRow.FindControl("lblTotalAmtFinanced")).Text;

    if (e.Row.RowType == DataControlRowType.DataRow)
    {        
        totalA += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "AmtFinanced"));
    }
    if (e.Row.RowType == DataControlRowType.Footer)
    {
        //Label lblTotal = (Label)e.Row.FindControl("lblTotal");

        if (totalAmtFinanced != null)
        {                   
            totalAmtFinanced = String.Format("{0:c}", totalA);
        }
    }
}

我添加的网格视图中的列称为 AmtFinanced。这就是我汇总一列的方式。如果您有任何问题,请告诉我!

于 2013-10-30T15:29:07.450 回答
1

嗨,在您的 gridview 中执行此操作

<asp:TemplateField HeaderText="Amount">
    <ItemTemplate>
        <asp:Label ID="lblAmount" runat="server" 
                   Text='<%# Eval("Amount","0:N2}").ToString %>'>
        </asp:Label>
    </ItemTemplate>
    <FooterTemplate>
        <asp:Label ID="lblTotal" runat="server"></asp:Label>
    </FooterTemplate>
</asp:TemplateField>

现在像公开一样声明

Private grdTotal As Decimal = 0

在事件 RowDataBound 从您的 gridview 之后

If e.Row.RowType = DataControlRowType.DataRow Then
    Dim rowTotal As Decimal =
    Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "Amount"))
    grdTotal = grdTotal + rowTotal
End If
If e.Row.RowType = DataControlRowType.Footer Then
    Dim lbl As Label = DirectCast(e.Row.FindControl("lblTotal"), Label)
    lbl.Text = grdTotal.ToString("N2")
End If
于 2013-10-30T15:34:40.270 回答