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

时间:2021-05-20 14:47:43

I am trying to create a Grand Total of all values in a GridView table which will be displayed in the footer, I have started by creating the placeholder but not sure how to go about creating the grand total

我试图在GridView表中创建所有值的Grand Total,它将显示在页脚中,我已经开始创建占位符但不知道如何创建总计

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

2 个解决方案

#1


1  

Hi in your gridview do this

嗨,在你的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>

Now declare like public

现在宣布像公开一样

Private grdTotal As Decimal = 0

After in the event RowDataBound from your gridview

在您的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

#2


1  

If you are adding up just a single column, this should work..

如果你只加一个列,这应该工作..

Code-Behind C#

代码隐藏在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);
        }
    }
}

The column in my gridview that I am adding up is called AmtFinanced. This is how I total up a single column. If you have any problems, let me know!

我加入的gridview中的列称为AmtFinanced。这就是我总共单列的方式。如果您有任何问题,请告诉我!

#1


1  

Hi in your gridview do this

嗨,在你的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>

Now declare like public

现在宣布像公开一样

Private grdTotal As Decimal = 0

After in the event RowDataBound from your gridview

在您的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

#2


1  

If you are adding up just a single column, this should work..

如果你只加一个列,这应该工作..

Code-Behind C#

代码隐藏在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);
        }
    }
}

The column in my gridview that I am adding up is called AmtFinanced. This is how I total up a single column. If you have any problems, let me know!

我加入的gridview中的列称为AmtFinanced。这就是我总共单列的方式。如果您有任何问题,请告诉我!