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 his Griddados has the columns Amount, Valor and Partial value and in the Footer
will be summing all values of Partial value.
Like:
Class model:
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;
}
}
Loading in this example is manual, but you can use it in your own way. The only pecularity that before sending to GridView
Griddados I do a multiplication ValorParcial = (x.Valor * x.Quantidade)
and send the data to grid all formatted.
In the GridDados_RowDataBound
i do the proper sum operations and at the end I show in the Footer of Gridview. OBS: the variables of type decimal
total and value are placed outside the method GridDados_RowDataBound
.
Upshot:
The way to define values is correct. What exactly is happening with the
GridView
?– Leonel Sanches da Silva
(gvcarteira. Rows[i]. Cells[3]. Text), as in this case the i of the error was not declared, but I leave the error.
– user9090
The mistake then happens here:
double qt = double.Parse(gvcarteira.Rows[i].Cells[3].Text);
– Leonel Sanches da Silva
It is one of the cases depending on the type of loop I implement, if it is the foreach the error is in i that has not been stated. if use is the error in gvcarteira.Rows.Count - 1 .
– user9090
But it has been declared. I will put an answer to you.
– Leonel Sanches da Silva