在GridView表VS 2010中添加一个Grand Total页脚

时间:2013-10-30 15:07:01

标签: asp.net vb.net visual-studio-2010 datagridview

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

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

2 个答案:

答案 0 :(得分: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);
        }
    }
}

我正在添加的gridview中的列名为AmtFinanced。这就是我总计单列的方式。如果您有任何问题,请告诉我们!

答案 1 :(得分: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

来自gridview的事件RowDataBound

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