Error adding value sum into the footer of a gridview

1

I'm trying to include in the footer of a gridview the sum of two columns, but I'm having trouble creating the method that performs this function.

I have already set the ShowFooter property to true , now I have to implement the DataBound event but it is not working. Can anyone help me?

 protected void gvcarteira_DataBound(object sender, EventArgs e)
    {
                    double val1 = 0;
        double val2 = 0;
       // foreach (GridViewRow row in gvcarteira.Rows)

        for (int i; gvcarteira.Rows.Count - 1; i++)
        {
            // converte o preco e a qtde para multiplicar e somar no total         

            double qt = double.Parse(gvcarteira.Rows[i].Cells[3].Text);
            double qtd = double.Parse(gvcarteira.Rows[i].Cells[4].Text);


            val1 = val1 + qt;
            val2 = val2 + qtd;


        }
        gvcarteira.FooterRow.Cells[0].Text = "Valor das Ações";
        gvcarteira.FooterRow.Cells[1].Text = val1.ToString();

        gvcarteira.FooterRow.Cells[3].Text = "Valor Gasto";
        gvcarteira.FooterRow.Cells[4].Text = val2.ToString();
      }

I want it to look something like this:

    
asked by anonymous 16.06.2014 / 04:54

2 answers

1

Example:

GridView:

<asp:GridView ID="GridDados" AutoGenerateColumns="False" runat="server" ItemType="WebAppDiegoWebForms.Fulano" OnRowDataBound="GridDados_RowDataBound" ShowFooter="True">
    <Columns>
        <asp:BoundField DataField="Codigo" HeaderText="Código" DataFormatString="{0:0000}"/>
        <asp:BoundField DataField="Nome" HeaderText="Nome" />
        <asp:BoundField DataField="Data" DataFormatString="{0:d}" HeaderText="Data" />
        <asp:BoundField DataField="Quantidade" HeaderText="Quantidade" />
        <asp:BoundField DataField="Valor" DataFormatString="{0:N2}" HeaderText="Valor" />
        <asp:BoundField DataField="ValorParcial" DataFormatString="{0:N2}" HeaderText="Valor Parcial" />
    </Columns>
</asp:GridView>

In your GridDados you have the columns Amount , Amount and Amount and no Footer will be summing all ValueValue values.

How to:

Class Template:

public class Fulano
{
    public int Codigo { get; set; }
    public string Nome { get; set; }
    public DateTime Data { get; set; }
    public Decimal Valor { get; set; }
    public int Quantidade { get; set; }
}

Code of this WebForm

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        var valores = (new Fulano[] 
        {
            new Fulano { Codigo = 1,  Nome = "Fulano 1", Data = DateTime.Now, Valor = 100M, Quantidade = 2},
            new Fulano { Codigo = 2, Nome = "Fulano 2", Data = DateTime.Now, Valor = 200M, Quantidade = 1},
            new Fulano { Codigo = 3, Nome = "Fulano 3", Data = DateTime.Now, Valor = 100M, Quantidade = 1},
            new Fulano { Codigo = 4, Nome = "Fulano 4", Data = DateTime.Now, Valor = 150M, Quantidade = 1},
            new Fulano { Codigo = 5, Nome = "Fulano 5", Data = DateTime.Now, Valor = 300M, Quantidade = 1}
        })
        .Select(x => new
        {
            x.Codigo,
            x.Nome,
            x.Data,
            x.Valor,
            x.Quantidade,
            ValorParcial = (x.Valor * x.Quantidade)
        })
        .ToArray();

        GridDados.DataSource = valores;
        GridDados.DataBind();
    }
}

private decimal total = 0;
private decimal valor = 0;        
protected void GridDados_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        valor = 0;               
        if (decimal.TryParse(e.Row.Cells[5].Text, out valor)){
            total += valor;
        }

    } else if (e.Row.RowType == DataControlRowType.Footer)
    {
        e.Row.Cells[4].Text = "Total:";
        e.Row.Cells[5].Text = total.ToString("N2");
        total = 0;
    }            
}

The load in this example is manual, but you can use it your way. The only requirement is that before sending to% GridData I do a GridView multiplication and send the data to all formatted grid.

No ValorParcial = (x.Valor * x.Quantidade) I do the proper sum operations and in the end I show in the GridView Footer. Note: Variables of type GridDados_RowDataBound total and value are placed outside the decimal / p>

Result:

    
16.06.2014 / 15:34
0

The problem is not the footer itself. It is the loop iteration. Consider changing for to foreach , which is safer:

foreach (GridViewRow linha in gvcarteira.Rows)
{
    // converte o preco e a qtde para multiplicar e somar no total         

    double qt = double.Parse(linha.Cells[3].Text);
    double qtd = double.Parse(linha.Cells[4].Text);

    val1 = val1 + qt;
    val2 = val2 + qtd;

}
    
16.06.2014 / 05:56